如何绘制共享相同x轴的2个子图

时间:2020-07-31 08:07:55

标签: python-3.x seaborn

因此,目前我有一个scatterplot和一个kdeplot,我使用seaborn库进行了规划。 . . 这是我绘制图表的方式:

# plot a graph to see the zipcodes vs the density
plt.figure(figsize=(16,8))
sns.kdeplot(king['zipcode'], shade=True, legend=False)
plt.xlabel('Zipcode')
plt.ylabel('Density')
plt.figure(figsize=(16,8))
sns.scatterplot(king['zipcode'],king['price'])

但是当我尝试做一个子图时,我的kdeplot似乎不见了: . 我尝试过这样的方式:

f, axarr = plt.subplots(2, sharex=True)
sns.kdeplot(king['zipcode'], shade=True, legend=False)
sns.scatterplot(king['zipcode'],king['price'])

是否可以在子图中正确渲染两个图形?

1 个答案:

答案 0 :(得分:-1)

是的!感谢@DavidG,我设法正确地绘制了子图。因此,我将axis对象添加到了各个图中。在这里。

f, axarr = plt.subplots(2, figsize=(16,8), sharex=True)
sns.kdeplot(king['zipcode'], shade=True, legend=False,ax=axarr[0])
axarr[0].set_ylabel('Density')
sns.scatterplot(x=king['zipcode'],y=king['price'],ax=axarr[1])
plt.show()

结果如下: .

相关问题