2016-08-31 8 views
1

各サブプロットの下にx軸ラベルを追加します。私はこのコードを使ってグラフを作成します:各matplotlibサブプロットのx軸ラベルを表示する方法

fig = plt.figure(figsize=(16,8)) 
ax1 = fig.add_subplot(1,3,1) 
ax1.set_xlim([min(df1["Age"]),max(df1["Age"])]) 
ax1.set_xlabel("All Age Freq") 
ax1 = df1["Age"].hist(color="cornflowerblue") 

ax2 = fig.add_subplot(1,3,2) 
ax2.set_xlim([min(df2["Age"]),max(df2["Age"])]) 
ax2.set_xlabel = "Survived by Age Freq" 
ax2 = df2["Age"].hist(color="seagreen") 

ax3 = fig.add_subplot(1,3,3) 
ax3.set_xlim([min(df3["Age"]),max(df3["Age"])]) 
ax3.set_xlabel = "Not Survived by Age Freq" 
ax3 = df3["Age"].hist(color="cadetblue") 

plt.show() 

これは見た目です。唯一の最初のものは、私は、各subplotの下で異なるx軸ラベルを表示することができますどのように

enter image description here

示して?

+0

この投稿の名前は非常に混乱しています。これは軸の「タイトル」を指しますが、実際には、「xラベル」(x軸の下のテキスト)です。 – pathoren

答えて

3

ファンクション(最初の呼び出しは正しく、その他は正しくありません):

fig = plt.figure(figsize=(16,8)) 
ax1 = fig.add_subplot(1,3,1) 
ax1.set_xlim([min(df1["Age"]),max(df1["Age"])]) 
ax1.set_xlabel("All Age Freq") # CORRECT USAGE 
ax1 = df1["Age"].hist(color="cornflowerblue") 

ax2 = fig.add_subplot(1,3,2) 
ax2.set_xlim([min(df2["Age"]),max(df2["Age"])]) 
ax2.set_xlabel = "Survived by Age Freq" # ERROR set_xlabel is a function 
ax2 = df2["Age"].hist(color="seagreen") 

ax3 = fig.add_subplot(1,3,3) 
ax3.set_xlim([min(df3["Age"]),max(df3["Age"])]) 
ax3.set_xlabel = "Not Survived by Age Freq" # ERROR set_xlabel is a function 
ax3 = df3["Age"].hist(color="cadetblue") 

plt.show() 
0

あなたが使用して各プロットの上にタイトルを追加することができます簡単です

ax.set_title('your title') 
0

は、ちょうどmatplotlib.axes.Axes.set_titleを使用し、ここにあなたのコードのうち、少し例です:あなたは間違っax.set_xlabel使用している

from matplotlib import pyplot as plt 
import pandas as pd 

df1 = pd.DataFrame({ 
    "Age":[1,2,3,4] 
}) 

df2 = pd.DataFrame({ 
    "Age":[10,20,30,40] 
}) 

df3 = pd.DataFrame({ 
    "Age":[100,200,300,400] 
}) 

fig = plt.figure(figsize=(16, 8)) 
ax1 = fig.add_subplot(1, 3, 1) 
ax1.set_title("Title for df1") 
ax1.set_xlim([min(df1["Age"]), max(df1["Age"])]) 
ax1.set_xlabel("All Age Freq") 
ax1 = df1["Age"].hist(color="cornflowerblue") 

ax2 = fig.add_subplot(1, 3, 2) 
ax2.set_xlim([min(df2["Age"]), max(df2["Age"])]) 
ax2.set_title("Title for df2") 
ax2.set_xlabel = "Survived by Age Freq" 
ax2 = df2["Age"].hist(color="seagreen") 

ax3 = fig.add_subplot(1, 3, 3) 
ax3.set_xlim([min(df3["Age"]), max(df3["Age"])]) 
ax3.set_title("Title for df3") 
ax3.set_xlabel = "Not Survived by Age Freq" 
ax3 = df3["Age"].hist(color="cadetblue") 

plt.show() 
関連する問題