2017-06-28 10 views
1

私はパンダを使って2つのボックスプロットを作成しました。 各図はplt.gcf()パンダの図の名前ボックスプロット

と表示されます。プロットを表示しようとすると、最後のボックスプロットだけが表示されます。そのようなfig1が上書きされています。 両方のボックスプロットを表示する正しい方法は何ですか?

このサンプルコード

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

dates = pd.date_range('20000101', periods=10) 
df  = pd.DataFrame(index=dates) 
df['A'] = np.cumsum(np.random.randn(10)) 
df['B'] = np.random.randint(-1,2,size=10) 
df['C'] = range(1,11) 
df['D'] = range(12,22) 

# first figure 
ax_boxplt1 = df[['A','B']].boxplot() 
fig1 = plt.gcf() 


# second figure 
ax_boxplt2 = df[['C','D']].boxplot() 
fig2 = plt.gcf() 

# print figures 
figures = [fig1,fig2] 
for fig in figures: 
    print(fig) 

答えて

1

定義、二つの図を得るために別々

fig, axes = plt.subplots(2) 

dates = pd.date_range('20000101', periods=10) 
df  = pd.DataFrame(index=dates) 
df['A'] = np.cumsum(np.random.randn(10)) 
df['B'] = np.random.randint(-1,2,size=10) 
df['C'] = range(1,11) 
df['D'] = range(12,22) 

# first figure 
df[['A','B']].boxplot(ax=axes[0]) # Added `ax` parameter 

# second figure 
df[['C','D']].boxplot(ax=axes[1]) # Added `ax` parameter 

plt.show() 

enter image description here

1

をそれらのそれぞれの二つの軸とプロットとの図形を作成していますそれにプロットする前に図数値を使用して数値を列挙できます。

plt.figure(1) 
# do something with the first figure 

plt.figure(2) 
# do something with the second figure 

コンプリート例:

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

dates = pd.date_range('20000101', periods=10) 
df  = pd.DataFrame(index=dates) 
df['A'] = np.cumsum(np.random.randn(10)) 
df['B'] = np.random.randint(-1,2,size=10) 
df['C'] = range(1,11) 
df['D'] = range(12,22) 

# first figure 
fig1=plt.figure(1) 
ax_boxplt1 = df[['A','B']].boxplot() 

# second figure 
fig2=plt.figure(2) 
ax_boxplt2 = df[['C','D']].boxplot() 

plt.show() 

enter image description here

関連する問題