python 数学建模、时间戳_如何使用matplotlib在python中绘制时间戳?

本文介绍了如何在Python中使用matplotlib库处理时间戳数据,从字符串转换为datetime实例,再到matplotlib兼容的日期表示。重点讲解了利用matplotlib的dates模块进行日期格式化和轴限制设置,以及调整x轴刻度标签的显示方式。
摘要由CSDN通过智能技术生成

好吧,一个两步的故事让他们的情节非常好

步骤1:从string到datetime实例

步骤2:对于日期/时间,从datetime到matplotlib兼容的约定

像往常一样,魔鬼被详细地隐藏起来。

matplotlib日期几乎等于,但不等于:# mPlotDATEs.date2num.__doc__

#

# *d* is either a class `datetime` instance or a sequence of datetimes.

#

# Return value is a floating point number (or sequence of floats)

# which gives the number of days (fraction part represents hours,

# minutes, seconds) since 0001-01-01 00:00:00 UTC, *plus* *one*.

# The addition of one here is a historical artifact. Also, note

# that the Gregorian calendar is assumed; this is not universal

# practice. For details, see the module docstring.

因此,强烈建议重新使用自己的工具:from matplotlib import dates as mPlotDATEs # helper functions num2date()

# # and date2num()

# # to convert to/from.

管理轴标签和格式缩放(最小/最大)是一个单独的问题

不过,matplotlib也为这一部分带来了武器:from matplotlib.dates import DateFormatter, \

AutoDateLocator, \

HourLocator, \

MinuteLocator, \

epoch2num

from matplotlib.ticker import ScalarFormatter, FuncFormatter

例如,可以:aPlotAX.set_xlim( x_min, x_MAX ) # X-AXIS LIMITs ------------------------------------------------------------------------------- X-LIMITs

#lt.gca().xaxis.set_major_locator( matplotlib.ticker.FixedLocator( secs ) )

#lt.gca().xaxis.set_major_formatter( matplotlib.ticker.FuncFormatter( lambda pos, _: time.strftime( "%d-%m-%Y %H:%M:%S", time.localtime( pos ) ) ) )

aPlotAX.xaxis.set_major_locator( AutoDateLocator() )

aPlotAX.xaxis.set_major_formatter( DateFormatter( '%Y-%m-%d %H:%M' ) ) # ----------------------------------------------------------------------------------------- X-FORMAT

#--------------------------------------------- # 90-deg x-tick-LABELs

plt.setp( plt.gca().get_xticklabels(), rotation = 90,

horizontalalignment = 'right'

)

#------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值