[英]matplotlib.pyplot overlaying axis labels on bars
I have this code that generates this graph but the text is so awfully off. 我有这个代码生成这个图,但文本非常糟糕。 I tried horizontal and vertical adjustments and rotations to make it readable with no success.
我尝试了水平和垂直调整和旋转,使其可读,但没有成功。
Could someone please tell me how I could relocate the text and choose a better font/size so they are readable? 请问有人可以告诉我如何重新定位文本并选择更好的字体/大小以便它们可读?
Any suggestions on a better graph to use to show this data is very welcome. 有关用于显示此数据的更好图表的任何建议都是非常受欢迎的。
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()
Try some of the following to adjust the bottom margins: 尝试以下某些操作来调整底部边距:
plt.tight_layout()
or fig.savefig(bbox_inches='tight')
to try and let matplotlib fix it on its own. plt.tight_layout()
或fig.savefig(bbox_inches='tight')
尝试让matplotlib自行修复它。
You can manually adjust the margins with 您可以使用手动调整边距
#adjust these as needed
plt.subplots_adjust(bottom=0.1, top=0.9, left=0.05, right=0.95)
Edit: 编辑:
If you'd rather put the labels on the plot next to the bars, you can do something like 如果您宁愿将标签放在条形图旁边的图上,您可以执行类似的操作
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)
You may want to add a bit of a buffer to the y coordinate (like y+0.01
or something). 您可能想要在y坐标中添加一些缓冲区(例如
y+0.01
或其他)。
You could try a horizontal barplot. 你可以尝试一个水平的条形图。 It often works better for this kind of visualization.
它通常更适合这种可视化。 I've also made a few tweaks to sort the categories and use better colors:
我还做了一些调整来对类别进行排序并使用更好的颜色:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.