我可以使用 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 可以理解,否则我无法正确应用语法。
最佳答案
我还推荐 James Bednar 的答案,它使用 hvPlot 。 HvPlot 构建在 HoloViews 之上:
import hvplot.pandas
df.hvplot.box()
但是,如果您想在 HoloViews 而不是 hvPlot 中执行此操作,则必须 melt您的数据以获取一列中的所有列名称以及另一列中的所有值。
此代码适用于您的示例数据:
hv.BoxWhisker(df.melt(), kdims='variable', vdims='value')
关于pandas - HoloViews:为 pandas 数据框中的每一列创建箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61757845/