1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Matplotlib show time ticks every two hours with minute offset

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make a graph with Matplotlib. I want the X-axis ticks to be every 2 hours and aligned with my data.

    Using ser_major_locator(mdates.MinuteLocator()) may accept interval=120 to make ticks every two hours, but this does not align with the data. MinuteLocator() will accept a range to byminute which satisfies the offset desire, but this will place ticks every hour.

    Here's an example:

    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    from datetime import datetime, timedelta

    start_time = datetime(2025, 9,15, 12, 37)
    t = [start_time, start_time + timedelta(hours=2), start_time + timedelta(hours=4)]
    values = [4,2,7]
    plt.plot(t,values)
    ax = plt.gca()
    ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=120))
    plt.grid(True)
    plt.show()

    Continue reading...

Compartilhe esta Página