1
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')

这是错误:

在此处输入图像描述

4

1 回答 1

2

错误原因:
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)


替代和更短的解决方案:
但是,在这种情况下,我将使用构建在 holoviews 之上的库 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'],
)


结果图: 多条曲线与 hvplot 叠加

于 2020-03-01T22:10:33.710 回答