如何为每个 matplotlib 子图显示 x 轴标签

Kab*_*ard 1 python matplotlib histogram

我想在每个子图下方添加一个 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()
Run Code Online (Sandbox Code Playgroud)

这是它的样子。只显示第一个

在此处输入图片说明

如何在每个下显示不同的 x 轴标签subplot

sas*_*cha 5

您使用ax.set_xlabel错误,这是一个函数(第一个调用是正确的,其他的不是):

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()
Run Code Online (Sandbox Code Playgroud)