如何将日期作为图表的限制

How to put dates as limits of a graph

几周前我写了这段代码,它可以工作,但我不得不同时重新安装 Anaconda,现在它不再工作了。我不明白为什么现在会出现这个错误。我的数据框的索引包含日期。感谢您的帮助!

当我写:

plt.figure(figsize=(13,10))
from matplotlib import cycler
colors = cycler('color',
                ['#FFBBBB', '#3388BB', '#9988DD',
                 '#EECC55', '#88BB44', '#EE6666'])
plt.rc('axes', facecolor='#E6E6E6', edgecolor='none',
       axisbelow=True, grid=True, prop_cycle=colors)
plt.rc('grid', color='w', linestyle='solid')
plt.rc('xtick', direction='out', color='gray')
plt.rc('ytick', direction='out', color='gray')
plt.rc('patch', edgecolor='#E6E6E6')
plt.rc('lines', linewidth=2)

plt.subplot(3, 1, 1)
l1 = plt.plot(data.index, data["E153"])
l2 = plt.plot(data.index, data["E159"])
l3 = plt.plot(data.index, data["E161"])
l4 = plt.plot(data.index, data["T13"])
l5 = plt.plot(data.index, data["T21"])
l6 = plt.plot(data.index, data["T22"])
plt.xlim(("1999-05-01","1999-11-30"))
plt.ylim(-0.1, 1.5)
plt.grid(True)    

plt.subplot(3, 1, 2)
plt.plot(data.index, data["E153"])
plt.plot(data.index, data["E159"])
plt.plot(data.index, data["E161"])
plt.plot(data.index, data["T13"])
plt.plot(data.index, data["T21"])
plt.plot(data.index, data["T22"])
plt.xlim(("2000-05-01","2000-11-30"))
plt.ylim(-0.1, 1.5)
plt.grid(True)    

plt.subplot(3, 1, 3)
plt.plot(data.index, data["E153"])
plt.plot(data.index, data["E159"])
plt.plot(data.index, data["E161"])
plt.plot(data.index, data["T13"])
plt.plot(data.index, data["T21"])
plt.plot(data.index, data["T22"])
plt.xlim(("2001-05-01","2001-11-30"))
plt.ylim(-0.1, 2)
plt.grid(True)

line_labels = ["E153", "E159", "E161", "T13", "T21", "T22"]
plt.figlegend([l1, l2, l3, l4, l5, l6],              
           labels= line_labels,       
           loc="center right", 
           bbox_to_anchor=(1.1, 0.5),         
           title="Légende") 

plt.tight_layout()
plt.show()

它说:

ConversionError: Failed to convert value(s) to axis units: '1999-05-01'

Here's a screen of the situation, you can see the all thing with my DataFrame and stuff

如果您的 x 轴是日期,您需要执行以下操作:

plt.xlim((pd.to_datetime("1999-05-01"),pd.to_datetime("1999-11-30")))

您的其他 .xlim() 个电话也是如此。