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

标签 python matplotlib pandas

给定这个数据框:

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/

相关文章:

python - matplotlib的这句话怎么理解?

python - 根据数据中的列数动态生成箱线图

python - Matplotlib,savefig() 的替代品以提高保存到 CString 对象时的性能?

python - 国际化 Django(在 OSX 上)

python - 字符串打印错误

python - 使用 pandas read_csv 时将分隔符限制为仅某些选项卡

python - 将系列拆分为大于阈值的段并将统计信息应用于段

python - 按另一列的顺序对包含 NA 的一列中的值进行排序

python - 将非空单元格移到 pandas DataFrame 的左侧

python - Fabric 2.x : tar and untar through pipe