2016-08-31 3 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() 
관련 문제