Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Scatter Plot in matplotlib with dates on x axis and values on y

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]
like image 607
Ravmcgav Avatar asked Jul 07 '16 23:07

Ravmcgav


3 Answers

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)
like image 173
Anthony Avatar answered Oct 12 '22 18:10

Anthony


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')
like image 40
Memin Avatar answered Oct 12 '22 18:10

Memin


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

like image 38
RSHAP Avatar answered Oct 12 '22 20:10

RSHAP