sam
sam

Reputation: 873

How do I plot two Pandas Series on a single plot figure?

I have two Series with the same indices and need to plot their values on the same plot, each as a line plot.

fig, axs = plt.subplots()
sns.lineplot(numkdhh_pmf.index, numkdhh_pmf.values, ax = axs, color = 'r')
sns.lineplot(biased.index, biased.values, ax = axs, color = 'b')
plt.show()

This only shows the latter plot, whichever is written second. Any thoughts on why both plots refuse to show on the figure?

The values of both Series are on a similar scale, so it isn't a zoom issue.

EDIT

The two plots show when omitting the fig, axs set up according to the below code. I'm still not sure why as I don't have a thorough understanding of Seaborn/matplotlib. The code is indeed being run from Jupyter but it was always in the same cell. Thanks for the help everyone.

ax2 = sns.lineplot(biased.index, biased.values, color = 'r')
sns.lineplot(numkdhh_pmf.index, numkdhh_pmf.values, ax = ax2, color = 'b')
plt.show()

Upvotes: 0

Views: 495

Answers (1)

Sheldore
Sheldore

Reputation: 39042

While your code works fine for me as it is, here is another alternative you can try. sns.lineplot returns an axis instance (let's call it ax1) which you can pass to the second lineplot. I chose some fake data to provide answer.

data = np.random.random((10))
ax1 = sns.lineplot(data, data, ax = axs, color = 'r')
sns.lineplot(data+0.5, data+0.8, ax = ax1, color = 'b')

enter image description here

Upvotes: 2

Related Questions