2016-06-16 3 views
1

私は複数のデータファイルを持っています。これらのファイルはすべて、データフレームを使って簡単に情報を処理しています。すべてのファイルはNetCDFファイルです。私は同じ数字上に4つのboxplotグラフをプロットしようとしているので、それらはすべて簡単に比較することができます(同じ時間選択で異なる時間に平均がどのようになっているかを見ています)。わずかに異なる時間間隔で撮影された観測データであり、異なる変数は異なる時間依存変数に対応するため、XとYの値が異なります。1つの図の4つのボックスプロットMathPlotLib

私はこれをmatplotlibを使って行うためにサブプロットを使用しようとしていますが、3つの空のグラフが吐き出されています。これをどうすればいいのですか?起きていない、正確に何

plt.figure(1) 
plt.subplot(2, 1, 1) 
ds1=xr.open_dataset(lfile1) 
one_day1 = ds1[variable1].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime)) 
df1 = one_day1.to_pandas().to_frame(name=variable1) 
df1.index=df1.index-pd.DateOffset(hours=7) 
df1.boxplot(column=variable1, by=df1.index.time, whis= [10, 90], sym='') 
plt.xlabel('Time') 
plt.ylabel('Temperature [degF]') 
plt.title('Daily Cycle') 
plt.suptitle('') 

plt.subplot(2, 1, 2) 
ds2=xr.open_dataset(lfile2) 
one_day2 = ds2[variable2].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime)) 
df2 = one_day2.to_pandas().to_frame(name=variable2) 
df2.index=df2.index-pd.DateOffset(hours=7) 
df2.boxplot(column=variable2, by=df2.index.time, whis= [10, 90], sym='') 
plt.xlabel('Time') 
plt.ylabel('Average Wind Speed') 
plt.title('Daily Cycle') 
plt.suptitle('') 

plt.subplot(2, 2, 1) 
ds3=xr.open_dataset(lfile3) 
one_day3 = ds3[variable3].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime)) 
df3 = one_day3.to_pandas().to_frame(name=variable3) 
df3.index=df1.index-pd.DateOffset(hours=7) 
df3.boxplot(column=variable3, by=df3.index.time, whis= [10, 90], sym='') 
plt.xlabel('Time') 
plt.ylabel('Wind Direction') 
plt.title('Daily Cycle') 
plt.suptitle('') 

plt.subplot(2, 2, 2) 
ds4=xr.open_dataset(lfile4) 
one_day4 = ds4[variable4].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime)) 
df4 = one_day4.to_pandas().to_frame(name=variable4) 
df4.index=df4.index-pd.DateOffset(hours=7) 
df4.boxplot(column=variable4, by=df4.index.time, whis= [10, 90], sym='') 
plt.xlabel('Time') 
plt.ylabel('Solar Radiation [W/m^2]') 
plt.title('Daily Cycle') 
plt.suptitle('') 

plt.show() 

は、ここでのコードですか?

さてさて、そのようなように、以下のように今、私はそれを編集した:今

plt.figure() 
ax1=plt.subplot(2,2,1) 
df1.boxplot(column=variable1, by=df1.index.time, whis= [10, 90], sym='') 
ax1.set_title('Daily Cycle') 


ax2=plt.subplot(2,2,2) 
df2.boxplot(column=variable2, by=df2.index.time, whis= [10, 90], sym='') 
ax2.set_title('Daily Cycle') 



ax3=plt.subplot(2,2,3) 
df3.boxplot(column=variable3, by=df3.index.time, whis= [10, 90], sym='') 
ax3.set_title('Daily Cycle') 


ax4=plt.subplot(2,2,4) 
df4.boxplot(column=variable4, by=df4.index.time, whis= [10, 90], sym='') 
ax4.set_title('Daily Cycle') 


plt.show() 

、私は5つの図と5つのウィンドウを取得しています。ウィンドウ全体を占める図は4つのサブプロットに含まれるすべてのデータを持ちますが、他の4つの図はそれぞれの位置にサブプロットがありますが空です。

+0

データで 'plt.boxplot()'を呼び出すことはありません。私はそれが空白である理由だと思う。 –

+0

今、私はdf.boxplot(...)を使用しています。これはplt.subplotのプロットではありませんか? –

+0

まあ、実際に 'plt'を使ってプロットする必要がある' plt'を使ってFigureとsuplotを作成したので、私は思っています。 –

答えて

1

subplotに間違った引数を与え、以前に作成した軸を上書きするため、グラフが3つしか表示されません。ドキュメントから :

subplot(nrows, ncols, plot_number) 

NROWSは、図中にサブプロットの行数であるとし、ncolsの図の列の数です。ここで

はパンダのデータフレームの箱ひげ図の方法を使用して作業例です:

df1 = pd.DataFrame({'a':[1,2,3],'b':[6,8,2],'c':[9,1,7]}) 
df2 = pd.DataFrame({'a':[15,23,32],'b':[6,80,2],'c':[9,10,7]}) 
df3 = pd.DataFrame({'a':[0.2,0.5,0.5],'b':[18,5,2],'c':[9,7,7]}) 
df4 = pd.DataFrame({'a':[51,32,20],'b':[4,3,20],'c':[7,2,1]}) 

fig = plt.figure() 
ax1 =fig.add_subplot(2,2,1) 
df1.boxplot(ax = ax1) 
ax2 =fig.add_subplot(2,2,2) 
df2.boxplot(ax = ax2) 
ax3 =fig.add_subplot(2,2,3) 
df3.boxplot(ax = ax3) 
ax4 =fig.add_subplot(2,2,4) 
df4.boxplot(ax = ax4) 
plt.show() 

enter image description here 私はあなたのプロットがすべて空になっている理由はわかりません。プロットしているDataFramesの内容を確認します。

+0

ああ、とてもいい。どうもありがとうございます。 –

+0

それで、それは私の問題の大半を解決しましたが、その後はもっと起きました。今、私は5つの数字があります。 1つはデータが入っていて、4つの別々のグラフがすべてダンプされたように見える大きな図形ですが、残りの4つはデータがない4つの異なるウィンドウのすべてのサブプロットです。オリジナルの投稿を編集してコードを更新しました。 –

+0

図にサブプロットを明示的に追加し、軸をboxplot呼び出しに渡してみます。私は、私が意味することを示すために私の例を修正しました。 – Molly

関連する問題