python - 如何显示每个 matplotlib 子图的 x 轴标签

标签 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()

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

enter image description here

如何在每个子图下显示不同的x轴标签?

最佳答案

您使用的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()

关于python - 如何显示每个 matplotlib 子图的 x 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39257712/

相关文章:

python - Matplotlib 导航工具栏 : remove "Edit curves lines and axes parameters"

python - Pip 不会安装 setup.py 中使用 install_requires 声明的依赖项

r - 如何使用ggplot绘制向量的直方图/频率计数?

image-processing - 在 SIMD 中矢量化直方图的方法?

python - 如何缩放直方图 Pandas 图的 y 轴?

python - 将 bash 指向安装在 Windows 上的 python

python - 无法设置 Celery 结果后端

python - 将 matplotlib 颜色图转换为 seaborn 调色板

python - 在将 csv 导入 db 之前跳过第一行并删除某些字符

python - 根据键的组合比较字典