2

If I run the following code:

import pandas as pd
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt

#df = pd.DataFrame(np.random.randn(3,1), index=[8,9,10], columns=['test'])
df = pd.DataFrame(np.random.randn(3,1), index=[datetime(2012,8,1),datetime(2012,9,1),datetime(2012,10,1)], columns=['test'])
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax1.invert_xaxis()
ax1.plot(df.index, df['test'])
fig.show()

I get an exception:

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)

It works fine if I disable the "invert_xaxis" command, and also if the index uses non-Datetime values.

I've seen some similar bugs reported (eg here and here) when plotting a dataframe with out-of-order date index but this was fixed in an earlier version of pandas.

Any suggestions on a workaround ? I'm using matplotlib 1.2.1 and pandas 0.11.0

Community
  • 1
  • 1
Geoff S
  • 166
  • 1
  • 8

2 Answers2

2

As a workaround: it does work for me when using the plot method of pandas, and when calling the invert_xaxis afterwards:

fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
df.plot(ax=ax1)
ax1.invert_xaxis()
fig.show()

UPDATE: This is now fixed since the release of pandas 0.12 (July 2013) (see https://github.com/pydata/pandas/pull/3991 and https://github.com/pydata/pandas/issues/3990). So the workaround is not needed anymore.

joris
  • 133,120
  • 36
  • 247
  • 202
0

Try to use: plt.gca().invert_xaxis()

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Yasser Sami
  • 101
  • 1
  • 5