简体   繁体   English

在 matplotlib 上为 set_xlim 使用 DateTimeIndex

[英]Using DateTimeIndex for set_xlim on matplotlib

How can I set x/y limits on matplotlib to certain datetime values?如何将 matplotlib 上的 x/y 限制设置为某些日期时间值?

I got a DateTimeIndex object (called time) and i want the plots to fit inside the first and last value of this index.我有一个 DateTimeIndex 对象(称为时间),我希望这些图适合该索引的第一个和最后一个值。

If I try ax.set_xlim(time[0],time[-1])如果我尝试ax.set_xlim(time[0],time[-1])

it throws me this error:它给我这个错误:

Cannot compare type Timedelta with type float

Any suggestions?有什么建议?

The time handling in matplotlib is gregorian, so it needs to be converted, I think it needs to be done with date2num(). matplotlib中的时间处理是gregorian,所以需要转换,我认为需要用date2num()来完成。

matplotlib API Overview: Dates matplotlib API 概述:日期

ax.set_xlim(date2num([series_.index.min(), series_.index.max()])) ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM