条形图不会填充轴

时间:2015-10-16 17:41:26

标签: python matplotlib

我试图制作一个堆积的条形图,其中列出了可变数量的"累加器",它们有一个人的名字和三个百分比,总是加起来100但是当我在列表中有大量条目时,所有条都挤在图的左侧。

以下是代码:

per_unreviewed = np.array([p.accum_per_unreviewed for p in accumulators])
per_reviewed = np.array([p.accum_per_reviewed for p in accumulators])
per_signed_off = np.array([p.accum_per_signed_off for p in accumulators])
fig = Figure(facecolor="w", figsize=(15, 7))
ax = fig.add_subplot(111)
ind = np.arange(len(accumulators))
logger.debug("len(acc) = %d, ind = %s", len(accumulators), ind)
width = 0.45
p1 = ax.bar(ind, per_signed_off, width, color="g")
p2 = ax.bar(ind, per_reviewed, width, color="b", bottom=per_signed_off)
p3 = ax.bar(ind, per_unreviewed, width, color="r",
            bottom=per_signed_off + per_reviewed)
ax.set_title(title)
ax.set_ylabel("Percent by status")
ax.set_yticks(np.arange(0, 101, 20))
ax.set_xticks(ind + width / 2.0)
ax.set_xticklabels(
    [p.person for p in accumulators],
    rotation='vertical', clip_on=False)

fig.tight_layout()
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])

if (len(p1) > 0 or len(p2) > 0 or len(p3) > 0):
    ax.legend(
        (p1[0], p2[0], p3[0]),
        ('Signed Off', 'Reviewed', 'Unreviewed'),
        loc="upper left", bbox_to_anchor=(1.05, 1), borderaxespad=0
    )

canvas = FigureCanvas(fig)
outstr = StringIO.StringIO()
canvas.print_png(outstr)

结果 big bar graph

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用x轴范围?你有刻度线和数字大小,但没有任何东西可以告诉绘图x的范围。

我自己不使用子图,但有没有像ax.set_xlim([])或ax.xlim()那样做?

保罗·汤姆林的更新:我尝试了这些建议并没有帮助,但他们确实指出了正确的想法:

ax.set_xbound(lower=0, upper=len(accumulators))