matplotlib"axis.invert_xaxis";使用日期时间时崩溃 [英] matplotlib "axis.invert_xaxis" crashes when using datetime
问题描述
如果我运行以下代码:
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()
我得到一个例外:
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)
如果禁用"invert_xaxis"命令,并且索引使用非Datetime值,也可以正常工作.
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.
有任何解决方法的建议吗?我正在使用matplotlib 1.2.1和pandas 0.11.0
Any suggestions on a workaround ? I'm using matplotlib 1.2.1 and pandas 0.11.0
推荐答案
作为一种解决方法:使用熊猫的plot
方法以及随后调用invert_xaxis
时,它确实对我有用:
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()
更新:自熊猫0.12(2013年7月)发布以来,此问题现已修复(请参见 https: //github.com/pydata/pandas/pull/3991 和 https://github .com/pydata/pandas/issues/3990 ).因此,不再需要解决方法.
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.
这篇关于matplotlib"axis.invert_xaxis";使用日期时间时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!