2016-05-26 9 views
2

のサブプロットと図にプロットを集約する方法:素晴らしいです私はすでにこれに関連した答えを読んだし、これに従っている

fig = plt.figure() 
fig.add_subplot(221) #top left 
fig.add_subplot(222) #top right 
fig.add_subplot(223) #bottom left 
fig.add_subplot(224) #bottom right 
plt.show() 

が、今私は4つの空のサブプロットと数字を持っています。

私が理解できないことは、私のプロットを示すように#top leftサブプロットを設定する方法です。xここでxはすでに私が生成したmatplotlib.axes.AxesSubplotです。

合計で私のコードは次のとおりです。

plt1 = plot.scatter(foo,bar) 
plt2 = plot.line(bar, baz) 
plt3 = plot.scatter(you, get) 
plt4 = plot.line(the, picture) 

fig = plt.figure() 
fig.add_subplot(221) #top left 
fig.add_subplot(222) #top right 
fig.add_subplot(223) #bottom left 
fig.add_subplot(224) #bottom right 

# Here is where I got lost 
# I want figure subplot 221 to show plt1 etc... 

答えて

4

あなたはadd_subplotによって返されたAxesのオブジェクトを追跡維持する必要があります。

fig = plt.figure() 
ax1 = fig.add_subplot(221) #top left 
ax2 = fig.add_subplot(222) #top right 
ax3 = fig.add_subplot(223) #bottom left 
ax4 = fig.add_subplot(224) #bottom right 

そして、あなたが行うことができます:

ax1.plot(...) 
ax2.boxplot(...) 
ax3.hist(...) 
# etc 

しかし、私はちょうどこれを行うだろう:

fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=2, ncols=2) 

、そこから行きます。

+0

私が抱いているのは、 'ax1.plot(...)'が私が達成できた多くの書式を取り除いたことです。つまり、インデックスnではなくx軸として日付スタンプのインデックスを表示し、軸のタイトルなど... 私が好きなプロットが生成されますプロット(タイトル= '作成日によるもののステータス') '私には素敵な折れ線グラフが表示され、その上に列の値でグループ分けされています。日付インデックス これを 'ax1.plot(df [['a'、 'b'、 'c']])'に置き換えると、x軸がインデックス値 –

+1

として表示されるので、 'df [['col1' 、 'col2']]。plot(ax = ax1) ' –

+1

@TylerWoodそして、軸上の書式を直接設定します(例えば' ax.set_xlabel(...) ') –

1

それがサブプロットを作成する前に描画する元本ですか?ノーならば、このコードを試してみてください。

fig = plt.figure() 
fig.add_subplot(221) #top left 
plt1 = plt.scatter(foo,bar) 
fig.add_subplot(222) #top right 
plt2 = plt.line(bar, baz) 
fig.add_subplot(223) #bottom left 
plt3 = plt.scatter(you, get) 
fig.add_subplot(224) #bottom right 
plt4 = plt.line(the, picture) 
+0

私は既にプロットのコードをどのように表示したいのかを設定しているからです。 軸のラベル、タイトル、凡例、軸のスケールなどを再度設定する必要があります。 –

+0

はい、わかりました。この答えを見てください。http://stackoverflow.com/a/16754215 – Serenity