2017-10-04 101 views
0

我正在繪製兩個數字,每個數字都有多個子圖。我需要在一個循環內完成。下面是當我只有一個數字我做什麼:在一個循環中用子圖繪製多個數字

fig, ax = plt.subplots(nrows=6,ncols=6,figsize=(20, 20)) 
fig.subplots_adjust(hspace=.5,wspace=0.4) 
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

for x in range(1,32): 
    plt.subplot(6,6,x) 
    plt.title('day='+str(x)) 
    plt.scatter(x1,y1) 
    plt.scatter(x2,y2) 
    plt.colorbar().set_label('Distance from ocean',rotation=270) 
plt.savefig('Plots/everyday_D color.png')  
plt.close() 

現在我知道,當你有多個數字,你需要做這樣的事情:

fig1, ax1 = plt.subplots() 
fig2, ax2 = plt.subplots() 

但我不知道該怎麼在循環中繪圖,每個子圖都在它的位置(因爲如果有兩個數字,你不能繼續做plt.scatter)。請具體說明我需要做什麼(關於是否爲fig1.scatter,ax1.scatter,fig.subplots_adjust,...以及如何在最後保存並關閉)

回答

1

每個pyplot函數都有其在面向對象API中的相應方法。如果你真的想在同一時間環比這兩個數字軸,這應該是這樣的:

import numpy as np 
import matplotlib.pyplot as plt 

x1 = x2 = np.arange(10) 
y1 = y2 = c = np.random.rand(10,6) 

fig1, axes1 = plt.subplots(nrows=2,ncols=3) 
fig1.subplots_adjust(hspace=.5,wspace=0.4) 

fig2, axes2 = plt.subplots(nrows=2,ncols=3) 
fig2.subplots_adjust(hspace=.5,wspace=0.4) 

for i, (ax1,ax2) in enumerate(zip(axes1.flatten(), axes2.flatten())): 
    ax1.set_title('day='+str(i)) 
    ax2.set_title('day='+str(i)) 
    sc1 = ax1.scatter(x1,y1[:,i], c=c[:,i]) 
    sc2 = ax2.scatter(x2,y2[:,i], c=c[:,i]) 
    fig1.colorbar(sc1, ax=ax1) 
    fig2.colorbar(sc2, ax=ax2) 

plt.savefig("plot.png") 
plt.show() 
plt.close() 

在這裏你遍歷兩個扁平軸陣列,使得ax1ax2matplotlib axes密謀。 fig1fig2是matplotlib數字(matplotlib.figure.Figure)。

爲了獲得索引,還使用了enumerate。太行

for i, (ax1,ax2) in enumerate(zip(axes1.flatten(), axes2.flatten())): 
    # loop code 

相當於這裏

for i in range(6): 
    ax1 = axes1.flatten()[i] 
    ax2 = axes2.flatten()[i] 
    # loop code 

i = 0 
for ax1,ax2 in zip(axes1.flatten(), axes2.flatten()): 
    # loop code 
    i += 1 

這都不再寫。

在這一點上,您可能會對以下事實感興趣:儘管使用面向對象API的上述解決方案肯定更通用且更可取,但仍然有可能實現純pyplot解決方案。這看起來像

import numpy as np 
import matplotlib.pyplot as plt 

x1 = x2 = np.arange(10) 
y1 = y2 = c = np.random.rand(10,6) 

plt.figure(1) 
plt.subplots_adjust(hspace=.5,wspace=0.4) 

plt.figure(2) 
plt.subplots_adjust(hspace=.5,wspace=0.4) 

for i in range(6): 
    plt.figure(1) 
    plt.subplot(2,3,i+1) 
    sc1 = plt.scatter(x1,y1[:,i], c=c[:,i]) 
    plt.colorbar(sc1) 

    plt.figure(2) 
    plt.subplot(2,3,i+1) 
    sc2 = plt.scatter(x1,y1[:,i], c=c[:,i]) 
    plt.colorbar(sc2) 

plt.savefig("plot.png") 
plt.show() 
plt.close() 
+0

感謝您的回答。我明白,枚舉基本上在for循環中創建索引。那麼ax1和ax2只是索引?你能解釋一下這個例子中的對象ax1,axes1和fig1是什麼類型的? –

+0

'ax1'和'ax2'是matplotlib軸。'enumerate'是一個簡單的方法來獲取循環中的索引。也許[這](https://www.saltycrane.com/blog/2008/04/how-to-use-py-sons-enumerate-and-zip-to/)有助於理解更好的枚舉。我也更新了答案。 – ImportanceOfBeingErnest

1

這是一個版本,顯示如何在兩個不同的數字上運行散點圖。基本上你可以參考使用plt.subplots創建的軸。

import matplotlib.pyplot as plt 
import numpy as np 

x1 = y1 = range(10) 
x2 = y2 = range(5) 

nRows = nCols = 6 
fig1, axesArray1 = plt.subplots(nrows=nRows,ncols=nCols,figsize=(20, 20)) 
fig1.subplots_adjust(hspace=.5,wspace=0.4) 
fig1.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

fig2, axesArray2 = plt.subplots(nrows=nRows,ncols=nCols,figsize=(20, 20)) 
fig2.subplots_adjust(hspace=.5,wspace=0.4) 
fig2.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

days = range(1, 32) 
dayRowCol = np.array([i + 1 for i in range(nRows * nCols)]).reshape(nRows, nCols) 
for day in days: 
    rowIdx, colIdx = np.argwhere(dayRowCol == day)[0] 

    axis1 = axesArray1[rowIdx, colIdx] 
    axis1.set_title('day=' + str(day)) 
    axis1.scatter(x1, y1) 

    axis2 = axesArray2[rowIdx, colIdx] 
    axis2.set_title('day=' + str(day)) 
    axis2.scatter(x2, y2) 

    # This didn't run in the original script, so I left it as is 
    # plt.colorbar().set_label('Distance from ocean',rotation=270) 

fig1.savefig('plots/everyday_D1_color.png') 
fig2.savefig('plots/everyday_D2_color.png') 
plt.close('all') 

當我把原代碼後plt.colorbar()引發一個錯誤,所以我離開它的答案。如果你有一個colorbar是如何工作的例子,我們可以看看如何使這兩個數字發生,但其餘的代碼應該按預期工作!

請注意,如果day每個都沒有出現在dayRolCol numpy會引發錯誤,您可以自行決定如何處理該情況。此外,使用numpy絕對不是唯一的方式來做到這一點,只是一種我很熟悉的方式 - 您真正需要做的就是找到一種方法將特定日期/情節與(x,y)指數聯繫起來您想要繪製的軸。

+0

如果你在已經有答案的地方回答問題,最好在你的答案有多遠的時候說清楚。只有兩次相同的解決方案沒有用處。另外一個答案顯示瞭如何使用colorbar,所以我認爲說「我們」可以看看它是如何工作的 - - 如果你願意的話,你自己可能會看一看。 – ImportanceOfBeingErnest

+0

感謝您的回覆。一個問題是軸1和軸2來自哪裏。它似乎沒有任何連接到你之前介紹的axesArray1(或者只是一個錯誤)? –

+0

@ImportanceOfBeingErnest你是對的,另一個答案出現在我編寫答案時,直到 – Eric

相關問題