gpt4 book ai didi

python - pandas - 与 matplotlib 进行绘图集成

转载 作者:行者123 更新时间:2023-11-30 23:33:44 25 4
gpt4 key购买 nike

给定这个数据框:

xlabel = list('xxxxxxyyyyyyzzzzzz')
fill= list('abc'*6)
val = np.random.rand(18)
df = pd.DataFrame({ 'xlabel':xlabel, 'fill':fill, 'val':val})

这就是我的目标:http://matplotlib.org/mpl_examples/pylab_examples/barchart_demo.png

应用于我的示例,Group 将是 xyzGender 将是 abcScores 将是 val >.

我知道在 pandas 中绘图与 matplotlib 的集成仍在进行中,那么是否可以直接在 matplotlib 中进行集成?

最佳答案

这是你想要的吗?

df.groupby(['fill', 'xlabel']).mean().unstack().plot(kind='bar')

df.pivot_table(rows='fill', cols='xlabel', values='val').plot(kind='bar')

您可以将其拆开并摆弄标签、列和标题,但我认为这基本上可以为您提供您想要的情节。

对于当前的误差线,您必须直接转到 mpl。

mean_df = df.pivot_table(rows='fill', cols='xlabel',
values='val', aggfunc='mean')
err_df = df.pivot_table(rows='fill', cols='xlabel',
values='val', aggfunc='std')

rows = len(mean_df)
cols = len(mean_df.columns)
ind = np.arange(rows)
width = 0.8 / cols
colors = 'grb'

fig, ax = plt.subplots()
for i, col in enumerate(mean_df.columns):
ax.bar(ind + i * width, mean_df[col], width=width,
color=colors[i], yerr=err_df[col], label=col)

ax.set_xticks(ind + cols / 2.0 * width)
ax.set_xticklabels(mean_df.index)
ax.legend()

但是会有一个增强,可能是在 0.13 中:issue 3796

关于python - pandas - 与 matplotlib 进行绘图集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com