1

我有一张如下表:

np.random.seed(42)
df = pd.DataFrame(np.random.randint(0, 1000, size=(24,53)))
df['hour'] = range(24)
for col in df.columns[:-1]:
    df.rename({col: str(col)}, axis=1, inplace=True)

我想绘制。列(除外hour)对应于一年中的星期,行对应于一天中的小时。直接使用 pandas 将是:

df[df.columns[:-1]].plot(legend=False);

产生:

使用熊猫绘图

但是,我想在游戏中添加交互性。我想用,bokeh但它变化太快,我现在不知道怎么做。我尝试使用holoviews

import holoviews as hv
hv.extension('bokeh')
%%opts Curve [tools=['hover']]
lines = [hv.Curve((df['hour'], df[col]), label=col) for col in df.columns[:-1]]

from functools import reduce
reduce(lambda x, y: x*y, lines) # I'm not sure this is a clean way

产生类似的东西:

在此处输入图像描述

几乎是我想要的。特别是,悬停工具显示其下方点的 x/y 坐标。我希望它显示相应曲线的星期。最后,有没有更蟒蛇的方式来做到这一点?也许直接用bokeh

4

1 回答 1

2

You could have a look at the new holoplot project which is meant to be (almost) a drop-in replacement for the pandas plotting API built on HoloViews and Bokeh. That will allow you do use the regular pandas API:

import holoplot.pandas
df[df.columns[:-1]].plot(legend=False)

enter image description here

Note that it is still in active development. Separately for the sake of completeness the simpler way to express your HoloViews code would be:

lines = [hv.Curve((df['hour'], df[col]), label=col) for col in df.columns[:-1]]
hv.Overlay(lines)
于 2018-05-28T13:11:29.720 回答