Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cannot plot bar chart with DatetimeIndex #9613

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

Closed
JohnsonY1995 opened this issue Oct 29, 2017 · 4 comments
Closed

Cannot plot bar chart with DatetimeIndex #9613

JohnsonY1995 opened this issue Oct 29, 2017 · 4 comments

Comments

@JohnsonY1995
Copy link

Bug report

Bug summary

matplotlib.pyplot.bar unable to use pandas.core.indexes.datetimes.DatetimeIndex as x

Code for reproduction

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas_datareader.data as web
style.use('ggplot')

start = dt.datetime(2017,10,1)
end = dt.datetime(2017,10,28)
df = web.DataReader('AAPL', 'yahoo', start, end)
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=5, colspan=1)
ax2 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
ax1.plot(df.index, df['Adj Close'])
ax2.bar(df.index, df['Volume'])
plt.show()

Output

Traceback (most recent call last):
  File "C:/Users/x/x/x/test.py", line 13, in <module>
    ax2.bar(df.index, df['Volume'])
  File "C:\Users\Johnson\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "C:\Users\Johnson\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 2129, in bar
    left = x - width / 2
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

Matplotlib version

  • Operating system: Windows 10 (15063.540)
  • Matplotlib (retrieved from pip) version: 2.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6.2
  • Other libraries: numpy 1.13.3, pandas 0.21.0, pandas-datareader=0.5.0
@tacaswell
Copy link
Member

Something is not going correct with the unit handling (which in what converts from datetime objcets / np.datetime values -> floats that we can plot). It looks like only one or neither of the values an being converted here as we attempt to center the bars.

@QuLogic
Copy link
Member

QuLogic commented Oct 29, 2017

Same problem as #9610 perhaps?

@dstansby
Copy link
Member

Yep, I'd guess it is the same root issue as #9610

@timhoffm
Copy link
Member

Works here on 2.2.2. Probably fixed by #9779.

Please reopen if there are still issues.

@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants