Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas erasing scatterplot on ax

I want to use pandas to plot lines for the ease of use with handling the dates on the x-axis. However, I also want to overlay the line plot with a scatterplot to show each individual point. I was able to to this sucessfully in an previous version of pandas (1.4.2 I believe?) but I am now on a new computer with version 1.5.3 and this is no longer working. See the MVP. Note that the red scatterplots aren’t showing up, but that removing the s.plot call shows the scatterplots. Somehow, pandas is erasing the scatter points.

s = pd.Series(np.random.randn(100))
s.index = pd.period_range(start = "2000", freq = "M", periods = 100).to_timestamp()

f, ax = plt.subplots()
s.plot(ax = ax)
ax.scatter(s.index, s, color = "red")
plt.show()

Pandas version = 1.5.3
Matplotlib version = 3.7.0

plotting using %matplotlib inline

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could reset_index and then .plot.scatter here:

s.plot(ax=ax)
s.reset_index(name='value').plot.scatter(x='index', y='value', ax=ax, color="red")
ax.set_xlabel('')
ax.set_ylabel('')

enter image description here

Also note that you can plot markers in your original plot call:

s.plot(ax = ax, marker='o', markerfacecolor='red', markeredgecolor='red')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading