Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Scatterplot with dates and numeric values

I want to produce a simple scatter plot with dates in the X axis and numeric values in the Y axis and I'm getting a type promotion error. the data comes from a pandas dataframe.

Here's a small dump of the values I have in

X (a Pandas Series):

....
179   2016-11-08 18:03:00
180   2016-11-08 18:16:00
181   2016-11-08 18:18:00
182   2016-11-08 18:19:00
183   2016-11-08 18:20:00
184   2016-11-08 18:21:00

Name: date, Length: 185, dtype: datetime64[ns]

The Y axis is not a problem:

....
180    18.266667
181    18.300000
182    18.316667
183    18.333333
184    18.350000

Length: 185, dtype: float64

If I do:

plt.scatter(X,Y)
plt.show()

I get a type promotion error. Thanks for the help!

like image 858
user3635284 Avatar asked Jun 25 '26 01:06

user3635284


1 Answers

You get this error because plt.scatter() will only accept lists as paramaters, you need to first convert your X data to a list using pandas.Series.tolist(), and then scatter it. Supposing your data is a Pandas DataFrame with df['dates'] being your dates column and df['values'] being your values column:

plt.scatter(df['dates'].tolist(), df['values'])
plt.show()

like image 125
Vinícius Figueiredo Avatar answered Jun 27 '26 16:06

Vinícius Figueiredo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!