3

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!

1

1 Answer 1

8

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()

Sign up to request clarification or add additional context in comments.

4 Comments

Why can scatter plots not deal with a pandas.Series of dates? It seems like such a basic thing...plotting dates using a scatter plot.
@J.A.Cado Perhaps one can use the Pandas' function for data visualization depending on how the data looks like, something like this.
Hmm maybe...although I prefer to avoid pandas' data visualization magic. Unless you want plot something quick and easy, pandas viz functionality doesn't let you do as much tweaking as matplotlib. Still though, why get the type promotion error? Whats happening with scatterplot so that it doesn't let you use pandas.series of dates? FYI, ur trick worked, so I am not looking for fix, I am just trying to understand how scattorplot works.
I don't know the answer for your question, maybe open an issue or ask something similar but not duplicate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.