[Python] Why can't I change my x tick lables in an histogram plot? [closed]

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a single histogram of the following DataFrame with the x tick labels ranging from Jan to Dec "month" (in Spanish: Ene to Dic) that shows the accumulated values of "duration". The results that I keep getting is 2 histograms; one that is empty with the proper month labels in the X axis, and the other with the values that I need but without the proper month labels (see image below).

    How can I do this?

    plt_users_durat.sample(5)

    # Result:
    month duration
    2195 12 355.20
    228 05 343.49
    623 08 255.84
    144 05 423.85
    249 06 305.84


    My code:

    fig, ax = plt.subplots()

    # This creates the sample DataFrame as showed above:
    plt_users_durat = df_users_durat[["month","duration"]]
    plt_users_durat = pd.DataFrame(plt_users_durat).sort_values(by="month").reset_index(drop = True)

    # Plotting:
    ticks = [1,2,3,4,5,6,7,8,9,10,11,12]
    labs = ["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"]
    plt_users_durat.plot("month", "duration", rot = 45, kind="hist")
    plt.title('Minutos por Usuario al Mes')
    plt.xlabel('Mes')
    plt.ylabel('Duración')
    ax.set_xticks(ticks)
    ax.set_xticklabels(labs)
    plt.show()


    Result:

    [​IMG]

    The desired outcome is the combination of the two charts.

    Continue reading...

Compartilhe esta Página