【问题标题】:pandas dataframe bar plot put space between bars熊猫数据框条形图在条形之间放置空间
【发布时间】:2017-12-17 04:34:55
【问题描述】:

所以我希望我的图像看起来像这样

但现在我的图像看起来像这样

如何在不将条宽变为 1 的情况下减少条之间的间距?

这是我的代码:

plot=repeat.loc['mean'].plot(kind='bar',rot=0,alpha=1,cmap='Reds',
       yerr=repeat.loc['std'],error_kw=dict(elinewitdh=0.02,ecolor='grey'),
       align='center',width=0.2,grid=None)

plt.ylabel('')
plt.grid(False)
plt.title(cell,ha='center')
plt.xticks([])
plt.yticks([])
plt.ylim(0,120)
plt.tight_layout()`

【问题讨论】:

  • 你为什么不想改变条的宽度?
  • 我通过其他方式解决了这个问题。但是你知道如何改变误差线,让它只显示上限而不是上下限吗?谢谢

标签: pandas matplotlib dataframe bar-chart seaborn


【解决方案1】:

如果 pandas 或 seaborn 的顶级函数没有给您想要的结果,请从头开始绘制! :)

import seaborn.apionly as sns
import scipy as sp
import matplotlib.pyplot as plt

# some fake data
data = sp.randn(10,10) + 1
data = data[sp.argsort(sp.average(data,axis=1))[::-1],:]
avg = sp.average(data,axis=1)
std = sp.std(data,axis=1)

# a practical helper from seaborn to quickly generate the colors
colors = sns.color_palette('Reds',n_colors = data.shape[0])

fig, ax = plt.subplots()

pos = range(10)
ax.bar(pos,avg,width=1)
for col,patch in zip(colors,ax.patches):
    patch.set_facecolor(col)
    patch.set_edgecolor('k')

for i,p in enumerate(pos):
    ax.plot([p,p],[avg[i],avg[i]+std[i]],color='k',lw=2, zorder=-1)

【讨论】:

  • 我通过使用其他人的解决方案解决了答案。您的解决方案实际上并没有真正帮助我。感谢您的回答。
  • 啊我误会你了。
  • @nonickname 如果您确实找到了解决方案,请考虑将您的解决方案添加为编辑或回复。
猜你喜欢
  • 2016-04-12
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 2019-08-03
  • 2017-04-18
  • 1970-01-01
相关资源
最近更新 更多