简体   繁体   English

如何从hv.Dataset创建分组条形图?

[英]How to create grouped barplot from hv.Dataset?

I would like to create a grouped bar plot from a pandas.DataFrame using Holoviews Datasets. 我想使用Holoviews数据集从pandas.DataFrame创建分组的条形图。

In particular I would like to have bars grouped by their original column. 特别是,我希望按其原始列对条进行分组。 Right now bars are being plotted on top of each other: 现在,条形图相互重叠:

import pandas as pd
import holoviews as hv
hv.extension('bokeh')

df = pd.DataFrame({'A': list(range(10,15)),
                   'B' : list(reversed(range(20,25)))})
ds = hv.Dataset(df, kdims='index')
ds.to.bars(vdims='A')*ds.to.bars(vdims='B').opts(alpha=.5)

This results in: current snippet 结果是: 当前代码段

However I would like to have them side-by-side, similar to the plot featured in the official docs (bottom of the page). 但是,我想让它们并排放置,类似于官方文档 (页面底部)中显示的情节。

For plotting directly with pandas DataFrames I would recommend the hvPlot library which is built on top of HoloViews. 对于直接使用pandas DataFrames进行绘图,我建议使用hvPlot库,该库基于HoloViews构建。 HoloViews deals well with so called tidy data , while hvPlot is built to deal well with both tidy and wide data. HoloViews可以很好地处理所谓的整洁数据 ,而hvPlot可以很好地处理整洁和宽泛的数据。 Using hvPlot generating the plot you want is as simple as: 使用hvPlot生成所需的绘图非常简单:

import hvplot.pandas
df.hvplot.bar()

条形图

Using HoloViews directly you would have to use pd.melt to convert the data into a so called tidy format. 直接使用HoloViews,您将必须使用pd.melt将数据转换为所谓的整齐格式。 That would look like this: 看起来像这样:

hv.Bars(pd.melt(df.reset_index(), ['index']), ['index', 'variable'], 'value')

melt_example

For a neat explanation for what pd.melt is doing see this visualization 有关pd.melt正在做什么的pd.melt说明,请参见此可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM