0

I currently have a function that creates a time series graph from time/date data that is in MM-DD-YYYY HH-MM format. I am unsure as to how to change the x axis ticks such that it displays hours as well as the date as it currently only shows dates.

The set_major_locator line I included only returns ticks that have the year even though I have specified the hour_locator and the data is hourly.

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()

enter image description here

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
JSC
  • 181
  • 2
  • 12
  • A [mcve] would be useful here. – ImportanceOfBeingErnest Oct 09 '18 at 20:56
  • I added an image and deleted a few lines of code that isn't as relevant. – JSC Oct 09 '18 at 21:08
  • I cannot give a complete solution without runnable code. So just to bring you on the right track here: The formatter for the ticks needs to be set to work with the locator. If you change the locator you need to inform the formatter about that, or, because that is non-obvious to do, set a new formatter as well. – ImportanceOfBeingErnest Oct 09 '18 at 21:18

1 Answers1

1

Use matplotlib.dates.DateFormatter. First import it at the top

import matplotlib.dates as mdates

then replace this line

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

by something like this

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

In a example with random numbers (since you haven't provided sample data), it looks like this

enter image description here

Here, the formatter is chosen as you wanted: dates + hours. For further info about how to format the date on the axis, check here

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41