簡體   English   中英

如何在 Python 中使用 matplotlib 在繪圖中的所有 x 值的 xaxis 中以小時和分鍾格式顯示標簽?

[英]How can I show labels in hours and minutes format in xaxis for all x values in a plot using matplotlib in Python?

我有一個由日期時間列和兩列中的值組成的數據框。 df.to_dict()在這個問題的末尾給出。 我想繪制兩列的線圖。 我想在所有行的 xlabels 中顯示所有小時和半小時值,如下面的 Excel 圖中所示: 在此處輸入圖片說明

我已將日期時間列設置為數據框中的索引。 當我嘗試:

fig, ax = plt.subplots(figsize = (12,8))
df["Profile 1 (kW)"].plot(ax = ax, color = "blue")
df["Profile2 (kW)"].plot(ax = ax, color = "red")
plt.show()

然后我得到了一個如圖所示的圖,但我沒有得到 xaxis 中的所有值: 在此處輸入圖片說明

我嘗試了下面的代碼,但后來我只得到了每小時的價值。 我還想顯示分鍾和小時。

fig, ax = plt.subplots(figsize = (12,8))
df["Profile 1 (kW)"].plot(ax = ax, color = "blue")
df["Profile2 (kW)"].plot(ax = ax, color = "red")
x = df.index
plt.xticks(x, x.hour, rotation = 90)
plt.show()

在此處輸入圖片說明

我嘗試了不同的事情,包括不將日期時間列設置為索引,但無法弄清楚。 如何使用 matplotlib 在繪圖中獲得旋轉 90 度的 xaxis 中的所有值?

我的數據df.to_dict()如下所示: df.to_dict()

    {'Profile 1 (kW)': {Timestamp('2017-01-26 00:00:00'): 3469.88623,
  Timestamp('2017-01-26 00:30:00'): 3252.681152,
  Timestamp('2017-01-26 01:00:00'): 3224.691895,
  Timestamp('2017-01-26 01:30:00'): 3198.656006,
  Timestamp('2017-01-26 02:00:00'): 3093.029785,
  Timestamp('2017-01-26 02:30:00'): 2942.820068,
  Timestamp('2017-01-26 03:00:00'): 3022.526855,
  Timestamp('2017-01-26 03:30:00'): 2978.788818,
  Timestamp('2017-01-26 04:00:00'): 2882.123047,
  Timestamp('2017-01-26 04:30:00'): 2852.830078,
  Timestamp('2017-01-26 05:00:00'): 2903.768799,
  Timestamp('2017-01-26 05:30:00'): 2981.911865,
  Timestamp('2017-01-26 06:00:00'): 3145.723145,
  Timestamp('2017-01-26 06:30:00'): 4088.824951,
  Timestamp('2017-01-26 07:00:00'): 4702.880859,
  Timestamp('2017-01-26 07:30:00'): 5222.25,
  Timestamp('2017-01-26 08:00:00'): 5822.64502,
  Timestamp('2017-01-26 08:30:00'): 5869.506348,
  Timestamp('2017-01-26 09:00:00'): 5846.080078,
  Timestamp('2017-01-26 09:30:00'): 5829.958008,
  Timestamp('2017-01-26 10:00:00'): 5564.408691,
  Timestamp('2017-01-26 10:30:00'): 5681.383789,
  Timestamp('2017-01-26 11:00:00'): 5677.146973,
  Timestamp('2017-01-26 11:30:00'): 6021.491211,
  Timestamp('2017-01-26 12:00:00'): 6263.496094,
  Timestamp('2017-01-26 12:30:00'): 6415.172852,
  Timestamp('2017-01-26 13:00:00'): 6358.316895,
  Timestamp('2017-01-26 13:30:00'): 6426.74707,
  Timestamp('2017-01-26 14:00:00'): 6283.019043,
  Timestamp('2017-01-26 14:30:00'): 6293.240234,
  Timestamp('2017-01-26 15:00:00'): 6152.050293,
  Timestamp('2017-01-26 15:30:00'): 6238.09082,
  Timestamp('2017-01-26 16:00:00'): 6492.696289,
  Timestamp('2017-01-26 16:30:00'): 6777.85498,
  Timestamp('2017-01-26 17:00:00'): 6941.486328,
  Timestamp('2017-01-26 17:30:00'): 7035.896484,
  Timestamp('2017-01-26 18:00:00'): 7035.896484,
  Timestamp('2017-01-26 18:30:00'): 7054.0,
  Timestamp('2017-01-26 19:00:00'): 7035.896484,
  Timestamp('2017-01-26 19:30:00'): 7035.896484,
  Timestamp('2017-01-26 20:00:00'): 7035.896484,
  Timestamp('2017-01-26 20:30:00'): 5982.958008,
  Timestamp('2017-01-26 21:00:00'): 5853.871094,
  Timestamp('2017-01-26 21:30:00'): 5314.336914,
  Timestamp('2017-01-26 22:00:00'): 5085.748047,
  Timestamp('2017-01-26 22:30:00'): 4794.423828,
  Timestamp('2017-01-26 23:00:00'): 4378.999023,
  Timestamp('2017-01-26 23:30:00'): 3991.338135},
 'Profile2 (kW)': {Timestamp('2017-01-26 00:00:00'): 1419.9161550499994,
  Timestamp('2017-01-26 00:30:00'): 942.0649834499991,
  Timestamp('2017-01-26 01:00:00'): 880.4886180499989,
  Timestamp('2017-01-26 01:30:00'): 823.2096622499994,
  Timestamp('2017-01-26 02:00:00'): 590.8319760499999,
  Timestamp('2017-01-26 02:30:00'): 260.370598649999,
  Timestamp('2017-01-26 03:00:00'): 435.7255300499992,
  Timestamp('2017-01-26 03:30:00'): 339.5018486499994,
  Timestamp('2017-01-26 04:00:00'): 126.83715244999894,
  Timestamp('2017-01-26 04:30:00'): 62.39262064999912,
  Timestamp('2017-01-26 05:00:00'): 174.45780684999863,
  Timestamp('2017-01-26 05:30:00'): 346.3725520499993,
  Timestamp('2017-01-26 06:00:00'): 706.7573680499991,
  Timestamp('2017-01-26 06:30:00'): 2781.5813412499997,
  Timestamp('2017-01-26 07:00:00'): 4132.504338849999,
  Timestamp('2017-01-26 07:30:00'): 5275.11644905,
  Timestamp('2017-01-26 08:00:00'): 6595.98549305,
  Timestamp('2017-01-26 08:30:00'): 6699.0804146499995,
  Timestamp('2017-01-26 09:00:00'): 6647.54262065,
  Timestamp('2017-01-26 09:30:00'): 6612.074066649999,
  Timestamp('2017-01-26 10:00:00'): 6027.865569249999,
  Timestamp('2017-01-26 10:30:00'): 6285.210784850001,
  Timestamp('2017-01-26 11:00:00'): 6275.889789649999,
  Timestamp('2017-01-26 11:30:00'): 7033.447113249998,
  Timestamp('2017-01-26 12:00:00'): 7565.857855849999,
  Timestamp('2017-01-26 12:30:00'): 7899.546723449999,
  Timestamp('2017-01-26 13:00:00'): 7774.46361805,
  Timestamp('2017-01-26 13:30:00'): 7925.010003050001,
  Timestamp('2017-01-26 14:00:00'): 7608.80834365,
  Timestamp('2017-01-26 14:30:00'): 7631.2949638499995,
  Timestamp('2017-01-26 15:00:00'): 7320.67709365,
  Timestamp('2017-01-26 15:30:00'): 7509.966253050001,
  Timestamp('2017-01-26 16:00:00'): 8070.098284850001,
  Timestamp('2017-01-26 16:30:00'): 8897.0,
  Timestamp('2017-01-26 17:00:00'): 9057.43637065,
  Timestamp('2017-01-26 17:30:00'): 9365.0,
  Timestamp('2017-01-26 18:00:00'): 9400.0,
  Timestamp('2017-01-26 18:30:00'): 9304.96644905,
  Timestamp('2017-01-26 19:00:00'): 9265.13871385,
  Timestamp('2017-01-26 19:30:00'): 9265.13871385,
  Timestamp('2017-01-26 20:00:00'): 9265.13871385,
  Timestamp('2017-01-26 20:30:00'): 6948.674066649999,
  Timestamp('2017-01-26 21:00:00'): 6664.68285585,
  Timestamp('2017-01-26 21:30:00'): 5477.707659850001,
  Timestamp('2017-01-26 22:00:00'): 4974.812152449999,
  Timestamp('2017-01-26 22:30:00'): 4333.8988706499995,
  Timestamp('2017-01-26 23:00:00'): 3419.96429965,
  Timestamp('2017-01-26 23:30:00'): 2567.1103460499994}}

您需要使用以下命令自定義 matplotlib x 軸:

  • 時間定位器,用於指定刻度之間的間距
  • 時間格式化程序,用於指定日期/時間刻度標簽的格式
  • 日期/時間刻度標簽旋轉的可選參數
ax.xaxis.set_major_locator(md.MinuteLocator(byminute = [0, 30]))
ax.xaxis.set_major_formatter(md.DateFormatter('%H:%M'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)

下面是一個隨機生成數據框的示例。 請注意,我使用 matplotlib 接口來繪制數據,而不是熊貓的。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md


df = pd.DataFrame({'Time': pd.date_range(start = '2021-01-01 00:00', end = '2021-01-02 00:00', freq = '5min')})
df['Profile 1 (kW)'] = 10000*np.random.random(len(df))
df['Profile 2 (kW)'] = 10000*np.random.random(len(df))
df = df.set_index('Time')


fig, ax = plt.subplots(figsize = (12, 8))

ax.plot(df.index, df['Profile 1 (kW)'], color = 'blue')
ax.plot(df.index, df['Profile 2 (kW)'], color = 'red')

ax.xaxis.set_major_locator(md.MinuteLocator(byminute = [0, 30]))
ax.xaxis.set_major_formatter(md.DateFormatter('%H:%M'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM