-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cannot plot bar graph with dates: "TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')" #13142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you make this self contained? Also, I suspect this has been fixed. But see #12862 for potentially similar issue. |
Try this: import pandas as pd
import json
import decimal
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.dates as mdate
from datetime import datetime
import matplotlib
def timestamp_to_datetime(raw):
return datetime.fromtimestamp(raw/1000)
df = pd.DataFrame([{
'timestamp': 1500854400000,
'activity': 5},
{
'timestamp': 1500940800000,
'activity': 6
}])
df['date'] = df['timestamp'].apply(timestamp_to_datetime)
plt.figure(figsize=(20,10))
plt.bar(df['date'], df['activity'])
plt.xlabel('date')
plt.ylabel('activity')
plt.legend()
plt.show(); I don't think it has been fixed. I'm using the latest version (2.2.3) and it's the same issue, but it doesn't work for me. |
I’ll look at it, but 3.0.2 is the latest version. |
The default width for a bar is a float
I think there is a question here about whether Matplotlib should do something about this - i.e. if the data on the xaxis is not a float, what width should bar use by default? But then the question is what value do you ascribe to the width? I think a second question is if matplotlib could supply a better error if the xaxis units are not numbers and width is not specified. My tendency would be to do this rather than try to guess what the user wants. |
+1 for the better error message. Probably something like
|
Yeah, I looked at doing that quickly. My only hesitation is if that is particularly more helpful than:
|
Well, maybe if we do
That explicitly tells the user to go and change the parameters. Whereas in the original traceback, he first has to realize that |
@timhoffm Thats a good point, and agree with that as a good reason for a better error message |
import pandas as pd
import json
import decimal
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.dates as mdate
from datetime import datetime
import matplotlib
def timestamp_to_datetime(raw):
return datetime.fromtimestamp(raw/1000)
df = pd.DataFrame([{
'timestamp': 1500854400000,
'activity': 5},
{
'timestamp': 1500940800000,
'activity': 6
}])
df['date'] = df['timestamp'].apply(timestamp_to_datetime)
plt.figure(figsize=(20,10))
plt.bar(df['date'], df['activity'], width=np.timedelta64(24, 'h'))
plt.xlabel('date')
plt.ylabel('activity')
plt.legend()
plt.show(); This works. Thanks so much! |
Bug report
Bug summary
Unable to plot bar graph. Plotting a line graph works fine.
Code for reproduction
Actual outcome
Expected outcome
Expected: a plot of the (date, activity) in bar graph form. Changing plt.bar to plt.plot, as shown below, works perfectly fine:
Matplotlib version
print(matplotlib.get_backend())
): module://ipykernel.pylab.backend_inlineThe text was updated successfully, but these errors were encountered: