4

これについてグーグル全体で検索してきましたが、探しているものを正確に見つけることができないようです。

したがって、基本的には 2 つのリストがあります。1 つのリストはタイムスタンプ データで構成され、2 つ目のリストはそれに対応する値で構成されます。

今私の問題は次のとおりです。私のタイムスタンプは次の形式です

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

PLOT を本当に素晴らしいものにするための2 段階のストーリー

ここに画像の説明を入力 ここに画像の説明を入力

ステップ 1 : astringからdatetimeインスタンスへ
ステップ 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 に答える
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 に答える