如何显示每个matplotlib子图的x轴标签 [英] How to display x axis label for each matplotlib subplot

查看:1137
本文介绍了如何显示每个matplotlib子图的x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个子图下方添加一个x轴标签.我使用以下代码创建图表:

I want to add an x axis label below each subplot. I use this code to create the charts:

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

这是它的外观.仅第一个显示

This is how it looks. Only the first one shows

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

How can I show a different x axis label under each subplot?

推荐答案

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

You are using ax.set_xlabel wrong, which is a function (first call is correct, the others are not):

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

这篇关于如何显示每个matplotlib子图的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆