我用这段代码从一个动态源生成一个条形图。我遇到的问题是,当我的条形图少于10个时,条形图的宽度会发生变化,并且与布局不同,这里是代码:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from Processesing import dataProcess
def chartmak (dic) :
z = 0
D={}
D=dic
fig, ax = plt.subplots()
n = len(D)
ax.barh(range(n), D.values(), align='center', fc='#80d0f1', ec='w')
ax.set_yticks(range(n))
#this need more work to put the GB or the TB
ax.set_yticklabels(['{:3d} GB'.format(e) for e in D.values()], color='gray')
ax.tick_params(pad=10)
for i, (label, val) in enumerate(D.items()):
z+=1
ax.annotate(label.title(), xy=(10, i), fontsize=12, va='center')
for spine in ('top', 'right', 'bottom', 'left'):
ax.spines[spine].set_visible(False)
ax.xaxis.set_ticks([])
ax.yaxis.set_tick_params(length=0)
plt.gca().invert_yaxis()
plt.savefig("test.png")
plt.show()
如果字典条目少于10个,可以按如下方式填充:
这将显示以下输出:
相关问题 更多 >
编程相关推荐