4

我一直在整个谷歌上搜索这个,但看起来我无法找到我正在寻找的东西。

所以,基本上,我有两个列表:一个列表包含时间戳数据,第二个列表包含对应的值。

现在我的问题是:我的时间戳采用以下格式

['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方法来绘制它?

4

2 回答 2

6

是的,使用 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()

在此处输入图像描述

于 2015-09-22T23:26:18.363 回答
6

好吧,一个两步的故事,让他们的情节真的很好

在此处输入图像描述 在此处输入图像描述

第 1 步:从 astringdatetime实例
第 2 步:从 adatetime到与日期/时间matplotlib兼容的约定float


像往常一样,魔鬼隐藏在细节中。

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'
                                            )

    #------------------------------------------------------------------
于 2015-09-22T23:37:38.747 回答