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
>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('')
Also note that you can plot markers in your original plot call:
s.plot(ax = ax, marker='o', markerfacecolor='red', markeredgecolor='red')
