[英]What's wrong with my vdims in my HoloViews Python code?
display(df_top5_frac.head())
下面的代碼產生一個錯誤。
%opts Overlay [width=800 height=600 legend_position='top_right'] Curve
hv.Curve((df_top5_frac['Blocked Driveway']) , kdims = ['Hour'], vdims = ['Fraction'], label = 'Blocked Driveway') *\
hv.Curve((df_top5_frac['HEAT/HOT WATER']) , kdims = ['Hour'], vdims = ['Fraction'], label = 'HEAT/HOT WATER') *\
hv.Curve((df_top5_frac['Illegal Parking']) , kdims = ['Hour'], vdims = ['Fraction'], label = 'Illegal Parking') *\
hv.Curve((df_top5_frac['Street Condition']) , kdims = ['Hour'], vdims = ['Fraction'], label = 'Street Condition') *\
hv.Curve((df_top5_frac['Street Light Condition']), kdims = ['Hour'], vdims = ['Fraction'], label = 'Street Light Condition')
這是錯誤:
你的錯誤原因:
vdim 應該是您希望在 y 軸上具有的列的名稱,但列名稱“分數”不存在,因此您會收到錯誤消息。
這是一個可能的解決方案:
當您將小時設置為索引時,您可以指定: kdim='hour'
和vdim='blocked_driveway'
,但在這種情況下,您並不真正需要它們,可以將它們排除在外:
# import libraries
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
# create sample data
data = {'hour': ['00', '01', '02'],
'blocked_driveway': np.random.uniform(size=3),
'illegal_parking': np.random.uniform(size=3),
'street_condition': np.random.uniform(size=3),}
# create dataframe and set hour as index
df = pd.DataFrame(data).set_index('hour')
# create curves:
# in this case the index is automatically taken as kdim
# and the series variable, e.g. blocked_driveway is taken as vdim
plot1 = hv.Curve(df['blocked_driveway'], label='blocked_driveway')
plot2 = hv.Curve(df['illegal_parking'], label='illegal_parking')
plot3 = hv.Curve(df['street_condition'], label='street_condition')
# put plots together
(plot1 * plot2 * plot3).opts(legend_position='top', width=600, height=400)
替代和更短的解決方案:
在這種情況下,但是我會用庫hvplot這是建立在holoviews的頂部。
它的語法更簡單,你需要更少的代碼來獲得你想要的圖:
import hvplot.pandas
# you don't have to set hour as index this time, but you could if you'd want to.
df.hvplot.line(
x='hour',
y=['blocked_driveway',
'illegal_parking',
'street_condition'],
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.