1

I am learning visualization techniques in Python. I would like to plot two graphs vertically. Here is my code:

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

But it got only an empty plot:

enter image description here

I follow the advice here: https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html

Can you help me with how to get the desired output, please?

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
vojtam
  • 1,157
  • 9
  • 34

2 Answers2

1

Try this:

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()
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
1

You can try something like this:

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

(Dummy data)

kde subplots

Corralien
  • 109,409
  • 8
  • 28
  • 52