8000 Cannot plot bar graph with dates: "TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')" · Issue #13142 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Cannot plot bar graph with dates: "TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')" #13142
Closed
@derekhuizhang

Description

@derekhuizhang

Bug report

Bug summary

Unable to plot bar graph. Plotting a line graph works fine.

Code for reproduction

from boto3.dynamodb.conditions import Key, Attr
import pandas as pd
import json
import decimal 
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
from datetime import datetime

def timestamp_to_datetime(raw):
    return datetime.fromtimestamp(raw/1000)

df = pd.DataFrame(table.query(KeyConditionExpression=Key('partition_key').eq(name))['Items'])
df['date'] = df['timestamp'].apply(timestamp_to_datetime) 

plt.figure(figsize=(20,10))
plt.bar(df['date'], df['activity'].values)
plt.show();

Actual outcome

TypeError                                 Traceback (most recent call last)
<ipython-input-13-f28db9680a8e> in <module>()
     26 
     27 plt.figure(figsize=(20,10))
---> 28 plt.bar(df['date'], df['activity'].values)

c:\python27\lib\site-packages\matplotlib\pyplot.pyc in bar(*args, **kwargs)
   2773                       mplDeprecation)
   2774     try:
-> 2775         ret = ax.bar(*args, **kwargs)
   2776     finally:
   2777         ax._hold = washold

c:\python27\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args, **kwargs)
   1865                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1866                         RuntimeWarning, stacklevel=2)
-> 1867             return func(ax, *args, **kwargs)
   1868 
   1869         inner.__doc__ = _add_data_doc(inner.__doc__,

c:\python27\lib\site-packages\matplotlib\axes\_axes.pyc in bar(self, *args, **kwargs)
   2262         if align == 'center':
   2263             if orientation == 'vertical':
-> 2264                 left = x - width / 2
   2265                 bottom = y
   2266             elif orientation == 'horizontal':

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

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:

from boto3.dynamodb.conditions import Key, Attr
import pandas as pd
import json
import decimal 
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
from datetime import datetime

def timestamp_to_datetime(raw):
    return datetime.fromtimestamp(raw/1000)

df = pd.DataFrame(table.query(KeyConditionExpression=Key('partition_key').eq(name))['Items'])
df['date'] = df['timestamp'].apply(timestamp_to_datetime) 

plt.figure(figsize=(20,10))
**plt.plot(df['date'], df['activity'].values)**
plt.show();

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.2.3
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 2.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0