gpt4 book ai didi

Python 显示时间跨度

转载 作者:行者123 更新时间:2023-12-01 06:33:33 24 4
gpt4 key购买 nike

我创建了一个图表,您可以在其中看到可视化数据和数据趋势。是否可以按时间跨度剪切图表?

这是我的图表代码

import matplotlib.pyplot as plt
import matplotlib.dates as mdates


fig, ax = plt.subplots()
ax.grid(True)
year = mdates.YearLocator(month=1)
month = mdates.MonthLocator(interval=3)
year_format = mdates.DateFormatter('%Y')
month_format = mdates.DateFormatter('%m')
ax.xaxis.set_minor_locator(month)
ax.xaxis.grid(True, which = 'minor')
ax.xaxis.set_major_locator(year)
ax.xaxis.set_major_formatter(year_format)
plt.plot(df.index, df['JAN'], c='blue')
plt.plot(decomposition.trend.index, decomposition.trend, c='red')

enter image description here

我有这段代码来缩短图表,但我不知道如何在上面的代码中使用它。


start_date = datetime(2004,1,1)
end_date = datetime(2008,1,1)
df[(start_date<=df.index) & (df.index<=end_date)].plot(grid='on')

enter image description here

最佳答案

您可以使用plt.xlim调整日期范围,

plt.xlim([datetime(2004, 1, 1), datetime(2008, 1, 1)])

这会给你一个看起来像这样的x轴

enter image description here

关于Python 显示时间跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com