2015-01-16 54 views
1

我想使兩個圖表的底部有一個小得多的高度。我嘗試了set_yscale(1,.5),但它並不成熟,正在尋找如何做到這一點。在文檔中找不到任何內容。Matplotlib - 共享x軸的垂直高度圖

import matplotlib.pyplot as plt 
import numpy as np 

# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 



# Two subplots, the axes array is 1-d 
f, axarr = plt.subplots(2, sharex=True) 
axarr[0].plot(x, y) 
axarr[0].set_title('Sharing X axis') 
axarr[1].scatter(x, y) 
axarr[1].set_yscale(1,.5) 

plt.show() 
+0

你的意思是每個小區下的空白? – elyase

+0

@elyase不,我的意思是圖形的實際高度。現在頂部和底部圖形的高度是相等的。我希望底部圖形的高度爲頂部圖形高度的一半。 – user2242044

回答

2

您可以實現例如通過使用GridSpec將子圖放置在圖中。這爲代碼添加了一些額外的開銷,但是在繪圖位置及其相對寬度和高度方面給予了充分的靈活性。

%matplotlib inline 
import matplotlib.pyplot as plt 
from matplotlib import gridspec 
import numpy as np 

# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

# create subplots' axes 
fig = plt.figure() 
top_pos, bot_pos = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) 
top_ax = fig.add_subplot(top_pos) 
bot_ax = fig.add_subplot(bot_pos, sharex=top_ax) 

# do the plotting 
top_ax.set_title('Sharing X axis') 
top_ax.plot(x, y) 
bot_ax.scatter(x, y) 

result

+0

很酷,謝謝! – user2242044