10

I am having trouble making a scatter plot that has from a date array and a bunch of PM 2.5 values. My lists would look like the following:

dates = ['2015-12-20','2015-09-12']  
PM_25 = [80, 55]
Jona
  • 1,218
  • 1
  • 10
  • 20
Ravmcgav
  • 183
  • 1
  • 1
  • 11

3 Answers3

13
import pandas as pd
dates = ['2015-12-20','2015-09-12']  
PM_25 = [80, 55]
dates = [pd.to_datetime(d) for d in dates]

plt.scatter(dates, PM_25, s =100, c = 'red')

s sets the size c sets the color

There are a whole bunch of other args as well: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter

RSHAP
  • 2,337
  • 3
  • 28
  • 39
  • This is great! Could I then make this more specific, giving months or even seasonal labels for each year? – Ravmcgav Jul 08 '16 at 22:49
  • 24
    When I try to make a scatterplot with `datetime` in the `x`-axis, I get `TypeError: invalid type promotion`. Your solution does not work. – dwanderson Oct 05 '16 at 16:15
  • 1
    I also got the `invalid type promotion` error like @dwanderson and @BCR. Changing the dates conversion to `dates = [d.to_pydatetime() for d in dates]` works for me. – Tiffany G. Wilson May 01 '18 at 17:47
  • 7
    @TiffanyG.Wilson and others having the invalid type of promotion error. Do this plt.scatter(list(df.time.values),list(df.y.values),color='r') – Apex Jul 23 '18 at 13:09
  • @Apex Thanks. It worked by using your piece of code. – renny Apr 30 '19 at 09:31
3

If a plot with data that contains dates, you can use plot_date

Similar to the plot() command, except the x or y (or both) data is considered to be dates, and the axis is labeled.

First convert list to date time, as @RSHARP showed,

dates = [pd.to_datetime(d) for d in dates]

then you can use plot_date

plt.plot_date(dates, PM_25, c = 'red')
Memin
  • 3,788
  • 30
  • 31
1

a pandas dataframe is more common usually. so it's efficient to me:

import pandas as pd
dates = ['2015-12-20','2015-09-12']  
PM_25 = [80, 55]
data = pd.DataFrame({'dates':pd.to_datetime(dates),'PM_25':PM_25})
data.plot(x='dates',y='PM_25',marker='o',linestyle='none')

and you can define more like this:

data.plot(x='dates',y='PM_25',marker='o',linestyle='none',color='red',ms=3)
Anthony
  • 111
  • 1
  • 3