1

I am trying to add a scatter plot to a line plot by using plandas plot function (in jupyter notebook).

I have tried the following code :

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# plot the line 
a = pd.DataFrame({'a': [3,2,6,4]})
ax = a.plot.line()

# try to add the scatterplot
b = pd.DataFrame({'b': [5, 2]})
plot = b.reset_index().plot.scatter(x = 'index', y = 'b', c ='r', ax = ax)
plt.show()

I also checked the following various SO answers but couldn't find the solution.

If anytone can help me, that ould be very appreciated.


EDIT:

somehow the accepted answers works, but i realise that in my case the reason it was not working might have to do with the fact i was using datetime.

like in this code, i cant see the red dots...

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime as dt
%matplotlib inline

fig, ax = plt.subplots()
# plot the line 
a = pd.DataFrame({'a': [3,2,6,4]}, index = pd.date_range(dt(2019,1,1), periods = 4))
plot = a.plot.line(ax = ax)

# try to add the scatterplot
b = pd.DataFrame({'b': [5, 2]}, index = [x.timestamp() for x in pd.date_range(dt(2019,1,1), periods = 2)])
plot = b.reset_index().plot.scatter(x = 'index', y = 'b', c ='r', ax = ax)
plt.show()

Any idea whats wrong here?

jim jarnac
  • 4,804
  • 11
  • 51
  • 88

1 Answers1

4

This should do it (just add fig, ax = plt.subplots() in the beginning):

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

fig, ax = plt.subplots()

# plot the line 
a = pd.DataFrame({'a': [3,2,6,4]})
a.plot.line(ax=ax)

# try to add the scatterplot
b = pd.DataFrame({'b': [5, 2]})
plot = b.reset_index().plot.scatter(x = 'index', y = 'b', c ='r', ax = ax)
plt.show()

Please let me know if you need anymore help.

Edit: This will work for datetimes:

import matplotlib.pyplot as plt
from datetime import datetime as dt
# %matplotlib inline

fig, ax = plt.subplots()
# plot the line 
a = pd.DataFrame({'a': [3,2,6,4]}, index = pd.date_range(dt(2019,1,1), periods = 4))
plot = plt.plot_date(x=a.reset_index()['index'], y=a['a'], fmt="-")

# try to add the scatterplot
b = pd.DataFrame({'b': [5, 2]}, index = pd.date_range(dt(2019,1,1), periods = 2))
plot = plt.scatter(x=b.reset_index()['index'], y=b['b'], c='r')
plt.show()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Elegant Code
  • 678
  • 6
  • 18
  • thax, but then is it not overwritten by `ax = a.plot.line()` ? – jim jarnac Dec 05 '19 at 17:38
  • @jimbasquiat, ooopppsss, sorry about that. I changed it in my Jupyter notebook code but then I only copied the one line and added it to yours, instead of the whole code with the additional change user1558604 made. I initially copied your code into the answer and already indented it, and didn't want to have to reindent it again. Silly mistake. I'm glad user1558604 and you caught it quickly. – Elegant Code Dec 05 '19 at 18:10
  • @Elegant Code if you still have a bit of time, could you look at my edit ? (cant seem to find the issue when i use datetime on the x axis...) – jim jarnac Dec 05 '19 at 18:22
  • 1
    @jimbasquiat ,added support for datetime – Elegant Code Dec 05 '19 at 19:50