BUG: matplotlib invert_xaxis crashes when using datetime #3990

Closed
jorisvandenbossche opened this Issue Jun 22, 2013 · 1 comment

Projects

None yet

2 participants

@jorisvandenbossche

From stackoverflow, a question from more than a month ago, but still an issue I think (tested with master).

Basicly, when using ax.invert_xaxis() with datetime x-values, the following exception is raised:

RuntimeError: MillisecondLocator estimated to generate 5270400 ticks from 2012-08-01 00:00:00+00:00 to 2012-10-01 00:00:00+00:00: 
exceeds Locator.MAXTICKS* 2 (2000)

At first it may not seem an error from pandas, as you can trigger it by using pure matplotlib code, but the error comes from pandas\tseries\converter.py. And more, it is by importing pandas that causes working matplotlib code to work no longer:

import numpy as np
import matplotlib.pyplot as plt
import datetime

x = np.array([datetime.datetime(2012, 8, 1, 0, 0),
              datetime.datetime(2012, 9, 1, 0, 0),
              datetime.datetime(2012, 10, 1, 0, 0)], dtype=object)
y = np.array([-1,0,1])

## Working matplotlib!
fig, ax = plt.subplots()
ax.plot(x,y)
ax.invert_xaxis()
plt.show()

import pandas as pd

## Same matplotlib not working anymore after importing pandas
fig, ax = plt.subplots()
ax.plot(x,y)
ax.invert_xaxis()
plt.show()

CAUSE of the bug:
When using ax.invert_xaxis() the left and right xlim are interchanged. Because of this the delta in PandasAutoDateLocator will be negative (https://github.com/pydata/pandas/blob/master/pandas/tseries/converter.py#L241).
This in turn triggers MilliSecondLocator instead of the normal AutoDateLocator, and causes the error as the datetime range is too big for MilliSecondLocator.

What I don't understand is why this is not happening when using pandas plot to create the axes object (the workaround I gave at stackoverflow):

df = pd.DataFrame(y, index=x, columns=['test'])

## This works as expected!
fig, ax = plt.subplots()
df.plot(ax=ax)
ax.invert_xaxis()
plt.show()
@jreback

closed via #3991

@jreback jreback closed this Jul 10, 2013
@yarikoptic yarikoptic added a commit to neurodebian/pandas that referenced this issue Jul 25, 2013
@yarikoptic yarikoptic Merge commit 'v0.12.0rc1-43-g7b2eaa4' into debian
* commit 'v0.12.0rc1-43-g7b2eaa4': (571 commits)
  PERF: add ix scalar get benchmark
  DOC: more prominent HDFStore store docs about storer/table formats
  BUG: invert_xaxis (negative tot_sec) triggers MilliSecondLocator (#3990)
  BUG: (GH4192) fixed broken unit test
  BUG: (GH4192) Fixed buglet in the broadcasting logic in Series.where
  CLN: Ignore warnings generated by 'DROP TABLE IF EXISTS' when table does not exist.
  DOC: more cookbook recipies
  DOC: update ipython_directive with changes from ipython to restart prompt number at 1 each page
  DOC: increased width of text area
  TST: fix ujson tests failures on 32-bit
  TST: raise when no data are found when trying to dld multiple symbols
  TST: Create a MySQL database and run MySQL tests on Travis.
  CLN: write the attributes in a HDFStore as strings
  TST: remove double call to yahoo finance
  DOC to_datetime warning about dayfirst strictness
  TST: to_datetime format fixes
  DOC: minor io/whatsnew doc edits
  BUG/TST: wrong parsing of microseconds with format arg (#4152)
  RLS: first release candidate for v0.12.0
  BLD: use the wheel url for scikits timeseries
  ...
3275685
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment