什么';HoloViews Python代码中的VDIM有什么问题?
下面的代码产生一个错误什么';HoloViews Python代码中的VDIM有什么问题?,python,holoviews,hvplot,Python,Holoviews,Hvplot,下面的代码产生一个错误 display(df_top5_frac.head()) 以下是错误: 错误原因: vdim应该是您希望在y轴上显示的列的名称,但列名称“分数”不存在,因此您会得到错误。 这里有一个可能的解决方案: 当您将小时设置为索引时,您可以指定:kdim='hour'和vdim='blocked\u driveway',但在这种情况下,您并不真正需要它们,可以将它们省略: %opts Overlay [width=800 height=600 legend_position='
display(df_top5_frac.head())
以下是错误:
错误原因:
vdim应该是您希望在y轴上显示的列的名称,但列名称“分数”不存在,因此您会得到错误。
这里有一个可能的解决方案:
当您将小时设置为索引时,您可以指定:
kdim='hour'
和vdim='blocked\u driveway'
,但在这种情况下,您并不真正需要它们,可以将它们省略:%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')
替代和较短的解决方案:
但在这种情况下,我将使用构建在HoloView之上的库。
它具有更简单的语法,并且您需要更少的代码来获得所需的绘图:
# 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)
结果图:
还可以重新标记y轴过程
ylabel='Your custom label'
作为方法或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'],
)