如何使用 matplotlib 在 python 中绘制时间戳?
How to plot timestamps in python using matplotlib?
我一直在整个 google 上搜索此内容,但似乎无法准确找到我要查找的内容。
所以,基本上,我有两个列表:一个列表包含时间戳数据,第二个列表包含与之对应的值。
现在我的问题是:我的时间戳格式如下
['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
那么,matplotlib
中使用的是哪种时间格式?我试图直接绘制它,但它给了我:
ValueError: invalid literal
可以用datetime.datetime.strptime
转换吗?如果不行,那还有什么办法呢?
将 timestamp
转换为正确格式后,我应该如何绘制新转换的时间戳及其对应的值?
我可以使用 matplotlib.pyplot.plot(time, data)
还是必须使用 plot_date
方法来绘制它?
是的,使用 strptime
import datetime
import matplotlib.pyplot as plt
x = ['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
y = range(4)
x = [datetime.datetime.strptime(elem, '%a %b %d %H:%M:%S %Y') for elem in x]
(fig, ax) = plt.subplots(1, 1)
ax.plot(x, y)
fig.show()
好吧,一个两步的故事 让他们的情节真的很好
步骤 1:从 string
到 datetime
实例
步骤 2:从 datetime
到 matplotlib
约定兼容 float
for dates/times
一如既往,恶魔隐藏的很详细
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.
因此,强烈建议重新使用他们的 "own" 工具:
from matplotlib import dates as mPlotDATEs # helper functions num2date()
# # and date2num()
# # to convert to/from.
管理轴标签、格式和比例(min/max)是一个单独的问题
不过,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'
)
#------------------------------------------------------------------
我一直在整个 google 上搜索此内容,但似乎无法准确找到我要查找的内容。
所以,基本上,我有两个列表:一个列表包含时间戳数据,第二个列表包含与之对应的值。
现在我的问题是:我的时间戳格式如下
['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
那么,matplotlib
中使用的是哪种时间格式?我试图直接绘制它,但它给了我:
ValueError: invalid literal
可以用datetime.datetime.strptime
转换吗?如果不行,那还有什么办法呢?
将 timestamp
转换为正确格式后,我应该如何绘制新转换的时间戳及其对应的值?
我可以使用 matplotlib.pyplot.plot(time, data)
还是必须使用 plot_date
方法来绘制它?
是的,使用 strptime
import datetime
import matplotlib.pyplot as plt
x = ['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
y = range(4)
x = [datetime.datetime.strptime(elem, '%a %b %d %H:%M:%S %Y') for elem in x]
(fig, ax) = plt.subplots(1, 1)
ax.plot(x, y)
fig.show()
好吧,一个两步的故事 让他们的情节真的很好
步骤 1:从 string
到 datetime
实例
步骤 2:从 datetime
到 matplotlib
约定兼容 float
for dates/times
一如既往,恶魔隐藏的很详细
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.
因此,强烈建议重新使用他们的 "own" 工具:
from matplotlib import dates as mPlotDATEs # helper functions num2date()
# # and date2num()
# # to convert to/from.
管理轴标签、格式和比例(min/max)是一个单独的问题
不过,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'
)
#------------------------------------------------------------------