如何在 Python 中使用 matplotlib 以 x 轴显示所有 x 值的小时和分钟格式的标签?
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 度的 x 轴中获取所有值?
我的数据框如下所示: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 轴:
- 一个时间定位器,用于指定刻度之间的间距
- 时间格式器,用于指定 date/time 刻度标签的格式
- date/time 刻度标签旋转的可选参数
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 接口来绘制数据,而不是 pandas' 接口。
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()
我有一个由日期时间列和两列中的值组成的数据框。 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 度的 x 轴中获取所有值?
我的数据框如下所示: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 轴:
- 一个时间定位器,用于指定刻度之间的间距
- 时间格式器,用于指定 date/time 刻度标签的格式
- date/time 刻度标签旋转的可选参数
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 接口来绘制数据,而不是 pandas' 接口。
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()