как создать аккуратную ось x индексов datetime данных для моего графика - PullRequest
0 голосов
/ 05 августа 2020

Я рисую фрейм данных, индекс которого имеет тип datetime (например, 2018-05-29 08:20:00).

Я разрезаю данные на основе последнего часа и последнего дня и на прошлой неделе и в прошлом месяце , а затем я рисую их.

Данные собираются каждые один минуэт . Итак, индекс каждой строки отличается только на одну минуту.

Когда я рисую данные за последний час, ось x строится следующим образом:

enter image description here

Or, for the last month it is like:

enter image description here

which is clean and readable. But, when I plot the last day data the x-axis index is like:

enter image description here

Why it is overlapped? how to fix it?

the codes to plot these time frames are the same, just the given dataframe is changed:

self.canvas.axes.plot(df_day.index, df_day.loc[:, item], linestyle="None", marker='.')
                      # or df_month or df_week or df_hour

how to make a the x-axis index as the format that I want? I want it to be printed as hour:minute for last hour, or day hour:minute for last day.

I tried the links, but none of them helped:

Я пробовал

self.canvas.axes.xaxis.set_major_formatter(self.major_formatter, self.canvas.axes.get_xticklabels())

@ticker.FuncFormatter
def major_formatter(x, pos):
    return datetime.datetime.fromtimestamp(x.day / 1e3)

, но он вернул int46 в переменной x, поэтому это не помогло.

1 Ответ

1 голос
/ 05 августа 2020

из первого ответа на Как построить день и месяц , который также является ответом от владельца вопроса. Я нашел решение:

import matplotlib.dates as mdates
import matplotlib.pyplot as plt 

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(date, price , label="Price")
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))

или в моем случае:

self.canvas.axes.xaxis.set_major_formatter(mdates.DateFormatter('%d-%b'))

из кодов форматов strftime () и strptime ()¶ , можно узнать о форматах даты и времени.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...