python - 条形图中的额外空白 matplotlib

标签 python matplotlib

我正在创建以下图,它似乎在右侧留下了一些额外的空白,我不确定为什么。

enter image description here

生成上述内容的代码如下:

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()

最佳答案

尝试设置Axesxlim:

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/

相关文章:

python - 在 Python 中减少路径名

python - winfo_x winfo_y 获取坐标

Python2 : Resize image plotted using Matplotlib

matplotlib - 基于色调/调色板的条形图中的边缘颜色

python - 如何在Python中将日期列绘制为x轴并绘制y轴上的数据

python - 当模块名称中包含 '-' 破折号或连字符时如何导入模块?

python - 如何用python中的数组值写入文件?

python - 具有多个模型的django rest framework嵌套字段

python - Imshow:范围和方面

python - imshow 如何处理带有 M x N x 4 输入的 alpha channel ?