2014-02-06 36 views
0

我完全新的Matplotlib和我寫了這個代碼繪製兩個系列,到目前爲止,是工作的罰款:Matplotlib:在劇情增加了第三次要情節

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

list1 = [1,2,3,4] 
list2 = [4,3,2,1] 

somecondition = True 
plt.figure(1) #create one of the figures that must appear with the chart 

gs = gridspec.GridSpec(3,1) 

if not somecondition: 
    ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there 
    ax.plot(list1) #populate the "main" subplot 
else: 
    ax = plt.subplot(gs[:2, :]) 
    ax.plot(list1) 
    ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there 
    ax.plot(list2) #populate the second subplot 
plt.show() 

我希望做的是加入第三系列此圖,讓我們說:

list3 = [4,1,2,4] 

重要的是,第一副區(列表1)必須是兩倍,比其他兩個大;爲此,我使用了gridspace,但由於我真的很新,所以我無法理解如何爲此示例代碼設置參數以獲取第三個參數。任何人都可以解釋我應該如何編輯塊somecondition == True以獲得3個子圖(前兩個比下面的其他兩個大兩倍),而不僅僅是兩個? P.S.該代碼是可執行的。

回答

0

爲了得到2:1分的比例,可以用4行,並且使曲線取2,1,1行分別爲:

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

list1 = [1,2,3,4] 
list2 = [4,3,2,1] 
list3 = [4,1,2,4] 

somecondition = True 
plt.figure(1) #create one of the figures that must appear with the chart 

gs = gridspec.GridSpec(4,1) 

if not somecondition: 
    ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there 
    ax.plot(list1) #populate the "main" subplot 
else: 
    ax = plt.subplot(gs[:2, :]) 
    ax.plot(list1) 
    ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there 
    ax.plot(list2) #populate the second subplot 
    ax = plt.subplot(gs[3, :]) #create the second subplot, that MIGHT be there 
    ax.plot(list3) 
plt.show() 
+0

非常感謝,它非常完美。 –

1

這與Matplotlib次要情節的例子

import matplotlib.pyplot as plt 
import numpy as np 

x,y = np.random.randn(2,100) 
fig = plt.figure() 
ax1 = fig.add_subplot(211) 
ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2) 
ax1.grid(True) 
ax1.axhline(0, color='black', lw=2) 

ax2 = fig.add_subplot(212, sharex=ax1) 
ax2.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2) 
ax2.grid(True) 
ax2.axhline(0, color='black', lw=2) 

plt.show() 

它使用了一個非常直接的語法pyplotadd_subplot