问题
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