2

我发现 pyplot 图有一个奇怪的行为。当数据点的数量超过一定数量时,线不会画到最后——最右边有我无法摆脱的空白。我想知道如何强制 pyplot 一直绘制。

我正在使用 Python 2.7.5 和 matplotlib 1.2.1。

from pylab import *
from random import random
bins = arange(0, 180, 5)
data = array([random() for i in xrange(len(bins))])
plot(bins[:10], data[:10], drawstyle="steps-mid") #draws till the end
title("some data")
figure()
plot(bins, data, drawstyle="steps-mid") #white space at the right
title("all data")
show()

其他活动drawstyle选项也会发生这种情况。我可以xlim在绘图后使用 a 来缩小 xaxis,以便切断的部分也被切断轴,但我不想这样做,因为我想保持我的轴原样。我考虑过添加一个额外的元素bins和一个零data并在之后使用xlim,但这让我觉得这是一个非常古怪的修复。

可以看到所有数据一些数据的图。

4

1 回答 1

1

使用您Axes的 'set_xlim()方法来避免您看到的自动范围行为。

data = array([random() for i in xrange(len(bins))])
plot(bins[:10], data[:10], drawstyle="steps-mid") #draws till the end
title("some data")

fig = figure()
ax = fig.add_subplot(111)
plot(bins, data)
ax.set_xlim(min(bins), max(bins)
ax.set_title("all data")
show()
于 2013-08-21T13:27:42.413 回答