简体   繁体   English

我的 HoloViews Python 代码中的 vdims 有什么问题?

[英]What's wrong with my vdims in my HoloViews Python code?

display(df_top5_frac.head())

输出

The code below produces an error.下面的代码产生一个错误。

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

Here is the error:这是错误:

在此处输入图片说明

Cause of your error:你的错误原因:
The vdim should be the name of the column you want to have on the y-axis, but the column name 'Fraction' doesn't exist, so you get the error. vdim 应该是您希望在 y 轴上具有的列的名称,但列名称“分数”不存在,因此您会收到错误消息。

Here's a possible solution:这是一个可能的解决方案:
When you set hour as the index, you could specify: kdim='hour' and vdim='blocked_driveway' , but in this case you don't really need them and can leave them out:当您将小时设置为索引时,您可以指定: 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)


Alternative and shorter solution:替代和更短的解决方案:
In this case however I would use library hvplot which is built on top of holoviews.在这种情况下,但是我会用库hvplot这是建立在holoviews的顶部。
It has even easier syntax and you need a lot less code to get the plot you want:它的语法更简单,你需要更少的代码来获得你想要的图:

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'],
)


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM