How to plot 2 subplots that share the same x-axis

拟墨画扇 提交于 2020-12-15 05:31:34

问题


So currently i have a scatterplot and a kdeplot that i used seaborn library to plot out. Here is how i plotted the graphs:

# 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'])

But when i try to do a subplot, my kdeplot seems to be gone: I tried to do in such a way:

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

Is it possible to render both graph in subplots properly?


回答1:


Yep! Credits to @DavidG, i managed to plot out the subplots properly. So i added the axes object into the respective graphs. Here it goes.

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

And it results in this:



来源:https://stackoverflow.com/questions/63188083/how-to-plot-2-subplots-that-share-the-same-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!