【问题标题】:Vertically display two plots in python [duplicate]在python中垂直显示两个图[重复]
【发布时间】:2021-11-12 04:40:08
【问题描述】:
我正在学习 Python 中的可视化技术。我想垂直绘制两个图表。
这是我的代码:
import seaborn as sns
import matplotlib.pyplot as plt
kernel_avg_es_100 = sns.kdeplot(data=avg_100_data, x="AVGT")
plt.xlabel("Average temperature estimates")
kernel_avg_se_100 = sns.kdeplot(data=avg_100_data, x="SE AVGT")
plt.xlabel("Average temperature SE")
(kernel_avg_es_100, kernel_avg_se_100) = plt.subplots(2)
plt.show()
但它只有一个空的情节:
我听从这里的建议:https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html
您能帮我解决如何获得所需的输出吗?
【问题讨论】:
标签:
python
matplotlib
seaborn
【解决方案1】:
你可以试试这样的:
fig, (ax1, ax2) = plt.subplots(2, 1)
sns.kdeplot(data=avg_100_data, x="AVGT", ax=ax1)
ax1.set_xlabel("Average temperature estimates")
sns.kdeplot(data=avg_100_data, x="SE AVGT", ax=ax2)
ax2.set_xlabel("Average temperature SE")
plt.tight_layout()
plt.show()
(虚拟数据)
【解决方案2】:
试试这个:
import seaborn as sns
import matplotlib.pyplot as plt
fig, axs = plt.subplots(ncols=1, nrows=2)
sns.kdeplot(data=avg_100_data, x="AVGT", ax=axs[0])
sns.kdeplot(data=avg_100_data, x="SE AVGT", ax=axs[1])
axs[0].set(xlabel="Average temperature estimates")
axs[1].set(xlabel="Average temperature SE")
plt.show()