为什么子图在 x 轴上产生空 space?

Why does subplot produce empty space on xaxis?

我在 x 轴和 y 轴上有值,并试图在子图上生成简单的折线图。这是显示问题的简单基本示例。

import matplotlib.pyplot as plt

x1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
      31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
      59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72]
y1 = [24.892730712890625, 25.268890380859375, 26.677642822265625, 28.294586181640625, 29.477203369140625,
      30.61334228515625, 31.656219482421875, 32.371551513671875, 31.412261962890625, 31.973724365234375, 31.563812255859375,
      30.72821044921875, 29.249237060546875, 26.759185791015625, 26.081024169921875, 25.27996826171875, 24.69805908203125,
      24.92388916015625, 24.76177978515625, 24.385498046875, 24.093231201171875, 23.92156982421875, 23.788543701171875,
      23.67657470703125, 23.581085205078125, 23.92095947265625, 25.90557861328125, 27.767333984375, 29.196136474609375,
      30.25726318359375, 31.262786865234375, 32.2996826171875, 32.92620849609375, 33.32098388671875, 33.228057861328125,
      30.495269775390625, 29.17010498046875, 28.04144287109375, 27.326202392578125, 24.904205322265625, 23.775054931640625,
      24.1328125, 24.195343017578125, 23.751312255859375, 23.55316162109375, 23.459228515625, 23.304534912109375,
      23.233062744140625, 23.093170166015625, 23.15887451171875, 25.13739013671875, 27.397430419921875, 28.923431396484375,
      29.945037841796875, 30.976715087890625, 31.93109130859375, 32.665435791015625, 32.701324462890625, 31.212799072265625,
      30.201507568359375, 29.591888427734375, 28.002410888671875, 27.72802734375, 27.371002197265625, 26.072509765625,
      25.39373779296875, 25.196044921875, 25.2684326171875, 24.815582275390625, 24.27130126953125, 23.758575439453125,
      23.49615478515625, 23.3907470703125]

plt.subplot(513)

plt.plot(x1, y1, 'b-')
plt.grid(True)
plt.show()

一切正常。但是输出图在 x 轴上有一些空的 space。这是显示问题的图像:-

感谢任何解决问题的帮助。

=您可以使用 xlim 在后台覆盖 "AutoLocator" 引入的此行为:

plt.subplot(513, xlim=(0,72))
# or
plt.subplot(513, xlim=(x1[0], x1[-1]))

您还可以调整定位器,如本例(及其他)所示: http://matplotlib.org/examples/pylab_examples/major_minor_demo2.html

默认情况下,在 matplotlib 版本 <2.0 中,matplotlib 将为轴选择 "even" 限制。例如:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)

x = np.linspace(2.1, 22.8, 1000)
y = np.random.normal(0, 1, x.size).cumsum()

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

如果您希望将限制严格设置为数据限制,您可以使用 ax.axis('tight')。但是,这会将 x 和 y-limits 都设置为 "tight",这通常不是您想要的。

在这种情况下,您更可能只想将 x-limits 设置为 "tight"。一个简单的方法是使用 ax.margins(x=0)margins 指定自动缩放应按数据范围的百分比填充内容。因此,通过设置 x=0,我们实际上使 x-limits 与 x-direction.

中的数据限制相同

例如:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)

x = np.linspace(2.1, 22.8, 1000)
y = np.random.normal(0, 1, x.size).cumsum()

fig, ax = plt.subplots()
ax.plot(x, y)
ax.margins(x=0)
plt.show()

您也可以使用 ax.autoscale(axis='x', tight=True) 来完成此操作。

但是,margins 的另一个优点是您通常也希望 y-axis 填充数据范围的百分比。因此,通常想要执行以下操作:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)

x = np.linspace(2.1, 22.8, 1000)
y = np.random.normal(0, 1, x.size).cumsum()

fig, ax = plt.subplots()
ax.plot(x, y)
ax.margins(x=0, y=0.05)
plt.show()