我正在创建以下图,它似乎在右侧留下了一些额外的空白,我不确定为什么。
生成上述内容的代码如下:
import matplotlib.pyplot as plt
d1 = (0,1,3,6,4,2,2,59,4,3,4,4,33,3,2,5,61)
d2 = (6,7,9,4,4,4,3,19,4,1,11,28,13,8,15,28,28)
d3 = (6,7,9,4,4,4,3,19,4,1,14,28,13,8,15,28,32)
N = len(d1)
ind = np.arange(N)
width = 0.2
font = {'family' : 'sans-serif', 'weight' : 'normal', 'size' : 10}
matplotlib.rc('font', **font)
fig,ax = plt.subplots()
rects1 = ax.bar(ind, MA_DPU, width, color='r', alpha=0.7)
rects2 = ax.bar(ind+width, res1, width, color='b', alpha=0.7)
rects3 = ax.bar(ind+2*width, res2, width, color='g', alpha=0.7)
ax.set_xticks(ind+width*1.5)
plt.show()
最佳答案
尝试设置Axes
的xlim
:
ax.set_xlim(0, 16.4)
plt.show()
为方便起见,您可以使用 "plt.autoscale()" :
plt.autoscale()
避免手动设置 xlim
。
具有默认值的可选参数是:
plt.autoscale(enable=True, axis='both', tight=None)
关于python - 条形图中的额外空白 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18061513/