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

HoloViews: create boxplots for every column in a pandas dataframe

我可以使用 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()

虽然,如果我尝试使用 HoloViews 的 BoxWhisker Element 和 Bokeh 作为后端来做同样的事情,它对单个列来说效果很好:

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

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

但是当我尝试添加另一列时,我收到以下错误:

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

我不明白 Tabular Data HoloViews 理解是否有问题,或者我无法正确应用语法。

我不确定如何从本机 HoloViews BoxWhisker 界面实现您想要的,该界面是为整理数据而设置的,而不是像那样的一组独立列。同时,您可以像使用本机 .plot() 调用一样使用 hvPlot:

我还推荐 James Bednar 的答案,,它使用了 hvPlot。 HvPlot 建立在 HoloViews 之上:

import hvplot.pandas
df.hvplot.box()


但是,如果您想在 HoloViews 而不是 hvPlot 中执行此操作,则必须 melt 您的数据 以获取一列中的所有列名,以及另一列中的所有值列。

此代码适用于您的示例数据:

hv.BoxWhisker(df.melt(), kdims='variable', vdims='value')