Python: график времени и суммарные значения - PullRequest
0 голосов
/ 22 декабря 2018

У меня есть фрейм данных с 1000 строками, как показано ниже

   start_time   val
0   15:16:25    0.01
1   15:17:51    0.02
2   15:26:16    0.03
3   15:27:28    0.04
4   15:32:08    0.05
5   15:32:35    0.06
6   15:33:02    0.07
7   15:33:46    0.08
8   15:33:49    0.09
9   15:34:04    0.10
10  15:34:23    0.11
11  15:34:32    0.12
12  15:34:32    0.13
13  15:35:53    0.14
14  15:37:31    0.15
15  15:38:11    0.16
16  15:38:17    0.17
17  15:38:29    0.18
18  15:40:07    0.19
19  15:40:32    0.20
20  15:40:53    0.21
...   ....      ..

Я хотел бы построить его со временем на оси x.Я использовал

plt.plot(df['start_time'].dt.total_seconds(),df['val'])
# generate a formatter, using the fields required
fmtr = mdates.DateFormatter("%H:%M")
# need a handle to the current axes to manipulate it
ax = plt.gca()
# set this formatter to the axis
ax.xaxis.set_major_formatter(fmtr)

И он работает нормально, но на оси X у меня есть метки, которые не показывают правильное время, см. Ниже: enter image description here

Любая помощь?заранее спасибо

1 Ответ

0 голосов
/ 22 декабря 2018

Вы можете преобразовать timedelta s в seconds:

plt.plot(df['start_time'].dt.total_seconds(),df['val'])

Решение для преобразования timedelta s в string s из здесь , необходимо только преобразовать наносекунды всекунд:

import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(df['start_time'], df['val'])    

def timeTicks(x, pos):
    seconds = x / 10**9
    d = datetime.timedelta(seconds=seconds)
    return str(d)

formatter = matplotlib.ticker.FuncFormatter(timeTicks)
ax.xaxis.set_major_formatter(formatter)
plt.xticks(rotation=90)
plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...