0

我目前有一个函数,可以从 MM-DD-YYYY HH-MM 格式的时间/日期数据创建时间序列图。我不确定如何更改 x 轴刻度,使其显示小时数和日期,因为它目前只显示日期。

我包含的 set_major_locator 行仅返回具有年份的刻度,即使我指定了 hour_locator 并且数据是每小时的。

def graph(region):

    fig = plt.figure(num=None, figsize=(60, 20), dpi=100, facecolor='w', edgecolor='k')

    df_da_region = df_da_abv_09[df_da_abv_09['Settlement Point'] == region]
    df_rt_region = df_rt_abv_09[df_rt_abv_09['Settlement Point Name'] == region]

    fig = plt.plot_date(x=list(df_da_region['DateTime']), y=list(df_da_region['Settlement Point Price']), xdate = True, fmt="r-", linewidth=0.7)
    fig = plt.plot_date(x=list(df_rt_region['DateTime']), y=list(df_rt_region['Settlement Point Price']), xdate = True, fmt="g-", alpha=0.5, linewidth=0.7)

    fig = plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=5))
    plt.show()

在此处输入图像描述

4

1 回答 1

1

使用matplotlib.dates.DateFormatter. 首先在顶部导入

import matplotlib.dates as mdates

然后替换这一行

fig = plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=5))

通过这样的事情

myFmt = mdates.DateFormatter('%y-%m-%d %H') # here you can format your datetick labels as desired
plt.gca().xaxis.set_major_formatter(myFmt)

在带有随机数的示例中(因为您没有提供示例数据),它看起来像这样

在此处输入图像描述

在这里,可以根据需要选择格式化程序:日期 + 小时。有关如何在轴上格式化日期的更多信息,请查看此处

于 2018-10-10T06:53:29.687 回答