HoloViews:为pandas数据框架中的每一列创建boxplots。

问题描述 投票:0回答:1

我可以用Pandas创建以下的boxplot。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

不过,如果我尝试用HoloViews的方法来做同样的事情的话 盒须元素 以Bokeh为后台,单列工作正常。

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理解,或者说我无法正确应用语法轴。

pandas dataframe bokeh holoviews
1个回答
1
投票

我还会推荐James Bednar的答案。 这是用 hvPlot. HvPlot是建立在HoloViews之上的。

import hvplot.pandas
df.hvplot.box()

然而,如果你想在HoloViews中做,而不是在HvPlot中做,你将不得不 融化 您的数据 来获取一列中的所有列名和另一列中的所有值。 这段代码对你的样本数据是有效的。

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

1
投票

我不知道如何从原生的HoloViews BoxWhisker接口中实现你想要的东西,它是为整洁的数据而设置的,而不是像这样的一组独立列。 同时你可以像使用原生.plot()调用一样使用hvPlot。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.