我的 HoloViews Python 代码中的 vdims 有什么问题?
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 轴上的列的名称,但是列名 'Fraction' 不存在,所以你得到错误。
这是一个可能的解决方案:
当您将小时设置为索引时,您可以指定: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。
它有更简单的语法,你需要更少的代码来获得你想要的情节:
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'],
)
结果图:
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 轴上的列的名称,但是列名 'Fraction' 不存在,所以你得到错误。
这是一个可能的解决方案:
当您将小时设置为索引时,您可以指定: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。
它有更简单的语法,你需要更少的代码来获得你想要的情节:
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'],
)
结果图: