[英]How can I adjust my plot to make it easier to read?
我正在生成一個包含許多元素的條形圖。 如下圖,生成的圖不是很容易閱讀。 如何調整它以使其看起來更好並將列移動得更遠一點?
這是代碼。
import numpy as np
import matplotlib.pyplot as plt
def barchart(Gbar, Vbar, Wbar, Rbar, Dbar, Nebar, Tbar, Abar):
N = 10
G = Gbar
ind = np.arange(N) # the x locations for the groups
width = 0.12 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, G, width, color='b')
V = Vbar
rects2 = ax.bar(ind + width, V, width, color='g')
W = Wbar
rects3 = ax.bar(ind + width*2, W, width, color='y')
R = Rbar
rects4 = ax.bar(ind + width*3, R, width, color='r')
D = Dbar
rects5 = ax.bar(ind + width * 4, D, width, color='orange')
N = Nebar
rects6 = ax.bar(ind + width * 5, N, width, color='black')
T = Tbar
rects7 = ax.bar(ind + width * 6, T, width, color='purple')
Ab = Abar
rects8 = ax.bar(ind + width * 7, Ab, width, color='cyan')
# add some text for labels, title and axes ticks
ax.set_ylabel('Char edit distance')
ax.set_xticks(ind + width/2)
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5', 'B1', 'B2',
'B3', 'B4', 'C1'))
ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0],rects6[0],rects7[0],rects8[0]),
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height), ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)
plt.savefig('plot.png')
plt.show()
注意:附加的圖像是整個圖像的一部分,但應該足以了解我的問題。
重用上一個答案中的部分代碼並實施我的建議,代碼如下所示
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29]
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18]
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17]
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14]
fig, ax = plt.subplots()
labels = ('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1')
y_pos = np.arange(len(labels))*4
rects1 = ax.barh(y_pos + width, Google)
rects2 = ax.barh(y_pos + 2*width, Voicebase)
rects3 = ax.barh(y_pos + 3*width, Watson)
rects4 = ax.barh(y_pos + 4*width, Remeeting)
# add some text for labels, title and axes ticks
ax.set_yticks(y_pos+2)
ax.set_yticklabels(labels)
ax.set_xlabel('Some label')
ax.set_ylabel('Another label')
ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting'))
plt.show()
結果如下
這應該為不斷改進繪圖的可視化提供一個很好的起點。 我明確刪除了數字,因為我發現它的信息太多並使情節混亂。
圖表上有很多條形這一事實意味着,無論您做什么,那里的值都可能有一些重疊。 話雖如此,有一些事情可以改善外觀。 一種是增加圖形大小。 下一步是減小標簽的字體大小。 從您之前的問題中獲取代碼並對其進行一些修改:
Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29]
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18]
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17]
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14]
ind = np.arange(1,80,8)# the x locations for the groups
width = 0.9 # the width of the bars
fig, ax = plt.subplots(figsize=(14,6)) #increase figure size
rects1 = ax.bar(ind, Google, width, color='b')
rects2 = ax.bar(ind + width, Voicebase, width, color='g')
rects3 = ax.bar(ind + width*2, Watson, width, color='y')
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r')
rects5 = ax.bar(ind + width*4, Google, width, color='orange')
rects6 = ax.bar(ind + width*5, Voicebase, width, color='black')
rects7 = ax.bar(ind + width*6, Watson, width, color='purple')
rects8 = ax.bar(ind + width*7, Remeeting, width, color='cyan')
# add some text for labels, title and axes ticks
ax.set_ylabel('Char edit distance')
ax.set_xticks(ind + width/2 )
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1'))
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom',fontsize=8) # decrease label size
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)
plt.subplots_adjust(left=0.08,right=0.95)
plt.show()
給出圖:
編輯
可以在此處找到圖例文檔。 可以使用ax.legend()
的loc=
參數移動圖例。 設置值 1 會將圖例放在右上角,2 將放在左上角等。
您可以使用bbox_to_anchor()
將圖例移到繪圖之外。 這將減小條形圖的大小,因此可能會導致標簽重疊更多。 您也可以嘗試減小圖例的字體大小以減少這種影響。 使用
ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0], rects6[0], rects7[0], rects8[0]),
bbox_to_anchor=(1, 1),fontsize=8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.