[Python] matplotlib bar chart with dates

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I know about plot_date() but is there a bar_date() out there?

    The general method would be to use set_xticks and set_xticklabels, but I'd like something that can handle time scales from a few hours out to a few years (this means involving the major and minor ticks to make things readable).

    I am plotting values associated with a specific time interval (that the bar spans). Here is the basic solution I used:

    import matplotlib.pyplot as plt
    import datetime
    t = [datetime.datetime(2010, 12, 2, 22, 0), datetime.datetime(2010, 12, 2, 23, 0),
    datetime.datetime(2010, 12, 10, 0, 0), datetime.datetime(2010, 12, 10, 6, 0)]
    y = [4, 6, 9, 3]
    interval = 1.0 / 24.0 #1hr intervals, but maplotlib dates have base of 1 day
    ax = plt.subplot(111)
    ax.bar(t, y, width=interval)
    ax.xaxis_date()
    plt.show()

    Continue reading...

Compartilhe esta Página