[Python] How do I plot hours and minutes when they are in different pandas columns?

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pandas' dataframe like:

    hour minute cnt
    0 0 904890
    0 30 880374
    1 0 848198
    1 30 816488
    2 0 791761


    I would like to plot cnt using minute as minor ticks, and hour as major tick in matplotlib. I've tried several things but they didn't work. Here's one:

    fig, ax = plt.subplots(figsize=(8,5))
    df.plot(kind='line', y='cnt', ax=ax)
    ax.set_xticks(df['minute'])
    ax.set_xticklabels(df['minute'])


    I'd like my x-axis to look like

    |---+---+---+---+---+---+---+---|
    0 15 30 45 0 30 45 0 15
    0 1 2 ...


    Thanks.

    Continue reading...

Compartilhe esta Página