Python matplotlib.pyplot在条形图上重叠轴标签

Python matplotlib.pyplot在条形图上重叠轴标签,python,matplotlib,Python,Matplotlib,我有这个代码生成这个图形,但是文本太离谱了。我尝试了水平和垂直调整和旋转,以使其可读,但没有成功 有人能告诉我如何重新定位文本并选择更好的字体/大小,使其可读吗 任何关于使用更好的图表来显示这些数据的建议都是非常受欢迎的 import matplotlib.pyplot as plt N = 13 ind = np.arange(N) width = 0.8 good=[0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48] bad=[0, 1, 28,

我有这个代码生成这个图形,但是文本太离谱了。我尝试了水平和垂直调整和旋转,以使其可读,但没有成功

有人能告诉我如何重新定位文本并选择更好的字体/大小,使其可读吗

任何关于使用更好的图表来显示这些数据的建议都是非常受欢迎的

import matplotlib.pyplot as plt

N = 13
ind = np.arange(N) 
width = 0.8

good=[0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48]
bad=[0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48]
keys=[u'gaming', u'recreation', u'business', u'computer_internet', u'unknown', u'culture_politics', u'science_technology', u'law_crime', u'sports', u'religion', u'weather', u'health', u'arts_entertainment']
c2='r'
c1='b'
p1 = plt.bar(ind, good, width, color=c1)
p2 = plt.bar(ind, bad, width, color=c2, bottom=good)

plt.ylabel('Number of URLs scanned')
plt.title('Category')
plt.xticks(ind+width/2., keys, rotation='vertical')
#plt.yticks(np.arange(0,81,10), rotation='vertical')
plt.legend( (p1[0], p2[0]), ('good', 'bad') )

plt.show()


尝试以下方法调整底部边距:

plt.tight\u layout()
fig.savefig(bbox\u inches='tight')
尝试让matplotlib自行修复

您可以使用手动调整页边距

#adjust these as needed
plt.subplots_adjust(bottom=0.1, top=0.9, left=0.05, right=0.95)
编辑:

如果您希望将标签放在条形图旁边的绘图上,可以执行以下操作

N = 13
width = 0.4
ind = np.arange(N) + 0.5 #this puts the bars on the right of each "bin"
p1 = plt.bar(ind, good, width, color=c1)
p2 = plt.bar(ind, good, width, color=c2, bottom=good)
for rect, key in zip(p1, keys):
    x, y = rect.get_xy()
    plt.text(x-width, y, text, va='bottom', ha='left', rotation=90)

您可能需要向y坐标添加一点缓冲区(如
y+0.01
或其他内容)。

您可以尝试水平条形图。它通常更适合这种可视化。我还对类别进行了一些调整,并使用了更好的颜色:

import numpy
import matplotlib.pyplot as plt

good = np.array([0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48])
bad = np.array([0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48])
keys = np.array([u'gaming', u'recreation', u'business', u'computer_internet',
                 u'unknown', u'culture_politics', u'science_technology',
                 u'law_crime', u'sports', u'religion', u'weather',
                 u'health', u'arts_entertainment'])
ind = np.arange(1, len(keys) + 1) 

order = np.argsort(good + bad)
good, bad, keys = good[order], bad[order], keys[order]

c2 = '#FF7777'
c1 = '#7777FF'
p1 = plt.barh(ind, good, color=c1, align="center", label="good")
p2 = plt.barh(ind, bad, color=c2, left=good, align="center", label="bad")

plt.xlabel('Number of URLs scanned')
plt.yticks(ind, keys)
plt.legend(loc="center right")
plt.tight_layout()

你能澄清一下“如此糟糕”是什么意思吗?只是当你保存图形时,它被剪掉了?不,即使它在底部看到,它也太长了,会使图形看起来不清晰。我不知道也许我们可以把标签垂直移动到吧台旁边?