0

我正在尝试为每个条创建一个带有值的条形图,但这些值超出了图的宽度。

条形图

这是代码:

fig, ax = plt.subplots()
width = 0.75
ind = np.arange(len(values))
ax.barh(ind, values, width, color = colors)
ax.set_yticks(ind)
ax.set_yticklabels(names)
plt.title('\'%s\' attribute distribution' % name)
plt.xlabel('Count')
plt.ylabel(name)

for i, v in enumerate(values):
  ax.text(v,i, ' '+str(v), va='center', fontweight='bold')

for i, r in enumerate(ratios):
  ax.text(r,i, ' '+str(r)+'%', color = 'white', fontweight='bold')

plt.show()
4

3 回答 3

3

plt.xlim(None, 1.2*max(values))似乎可以解决问题。

条形图

感谢ImportanceOfBeingErnest

于 2019-06-05T11:54:42.833 回答
0

您可以删除图表周围的这个“框”。尝试将此添加到您的代码中。

for spine in plt.gca().spines.values():
    spine.set_visible(False)

plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off', labelbottom='on')

于 2019-06-05T11:45:17.673 回答
0

看看现在添加这个是否有帮助

plt.gcf().subplots_adjust(right=1.7)
于 2019-06-05T11:54:56.783 回答