HoloViews:为pandas数据框中的每一列创建框线图

时间:2020-05-12 17:14:47

标签: pandas dataframe bokeh holoviews

我可以使用Pandas pandas.DataFrame.boxplot()方法创建以下箱形图:

import pandas as pd
import numpy as np

np.random.seed(1234)
df = pd.DataFrame(np.random.rand(10, 4),
                  columns=['Col1', 'Col2', 'Col3', 'Col4'])

df.plot.box()
plt.show()

enter image description here

尽管,如果我尝试使用以Bokeh为后端的HoloViews的BoxWhisker Element进行相同的操作,那么它对于单列也可以正常工作:

import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

hv.BoxWhisker(
    data=df['Col1'],
    vdims='Col1'
)

enter image description here

但是,当我尝试仅添加另一列时,出现以下错误:

hv.BoxWhisker(
    data=df[['Col1', 'Col2']]
)

DataError: None of the available storage backends were able to support the supplied data format. PandasInterface raised following error:

 unsupported operand type(s) for +: 'NoneType' and 'int'

PandasInterface expects tabular data, for more information on supported datatypes see http://holoviews.org/user_guide/Tabular_Datasets.html

我不了解HoloViews理解的Tabular Data是否存在问题,或者我无法正确应用语法。

2 个答案:

答案 0 :(得分:1)

我不确定如何从本地HoloViews BoxWhisker接口实现所需的功能,该接口是为整洁的数据而不是像这样的独立列设置的。同时,您可以像使用本机.plot()调用一样使用hvPlot:

enter image description here

答案 1 :(得分:1)

我还建议使用hvPlot的James Bednar的答案。 HvPlot构建在HoloViews之上:

request.user


但是,如果要在HoloViews中而不是hvPlot中执行此操作,则必须melt数据才能在一列中获取所有列名称,而在另一列中获取所有值。 br />
此代码适用于您的示例数据:

import hvplot.pandas
df.hvplot.box()