2016-01-11 42 views
1

我有x和y軸的值,並試圖在子圖上產生簡單的線圖。這是顯示問題的簡單和基本的例子。爲什麼子圖會在x軸上產生空的空間?

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軸上有一些空的空間。這裏是一個顯示問題的圖像: -

empty space on xaxis

任何有助於解決這一問題表示讚賞。

回答

2

=你可以使用XLIM覆蓋此行爲由「自動定位」的背景介紹:

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

你也可以ajust定位器像本例中所示(及其他): http://matplotlib.org/examples/pylab_examples/major_minor_demo2.html

+0

都正確。但我已經選擇了你給我一個簡單而具體的解決方案。 –

+0

您將'xlim'應用於'plt.subplot2grid'函數的位置? –

0

默認情況下,在matplotlib版本< 2.0中,matplotlib將爲軸選擇「偶數」限制。例如:

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

enter image description here

如果你喜歡有嚴格設定的數據限制的限制,你可以使用ax.axis('tight')。但是,這將使x和y限制都變得「緊密」,這通常不是你想要的。

在這種情況下,您更希望將x限制設置爲「緊」。一個簡單的方法是使用ax.margins(x=0)margins指定自動調節應按數據範圍的百分比填充事物。因此,通過設置x=0,我們有效地使x限制與x方向上的數據限制相同。

例如:

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

enter image description here

您還可以通過使用ax.autoscale(axis='x', tight=True)做到這一點。

但是,margins的另外一個優點是,您通常也希望y軸座標以數據範圍的百分比表示。因此,它是常見的想要做的事,如:

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

enter image description here

相關問題