gpt4 book ai didi

python - pyplot 轴中的日期时间格式

转载 作者:行者123 更新时间:2023-12-03 14:35:47 26 4
gpt4 key购买 nike

我正在尝试针对 datetime 的列表绘制一些数据pyplot 在 x 轴上的对象。然而,日期显示为标准格式,即 %Y-%m-%d %H:%M:%S (太长了)。我可以通过使用 strftime 创建日期字符串列表来规避此问题。并使用它。我也知道有某种date pyplot 的内在对象我可以用它代替 datetime .

有没有办法告诉pyplot以哪种格式绘制 datetime然而对象呢?无需将所有内容转换为字符串或其他类型的对象?

谢谢你。

最佳答案

您可以使用 DateFormatter :

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(your_dates, your_data)

# format your data to desired format. Here I chose YYYY-MM-DD but you can set it to whatever you want.
import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))

# rotate and align the tick labels so they look better
fig.autofmt_xdate()

关于python - pyplot 轴中的日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968654/

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