[Python] Matplotlib weekly bars are too thin when width<1.0, too thick when width>=1.0

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    Why are my bars so thin? I have tried setting width to 1 and they go really thick. I'm not sure what else to try. The default thickness is 0.8, is this how it should look?

    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import pandas as pd
    import numpy as np

    working_runs = pd.DataFrame(np.random.uniform(1, 2, 210),
    columns=['distance'],
    index=pd.date_range('2019-06-01', periods=210, freq='D'))
    summed = working_runs['distance'].resample('W').sum()
    df = pd.DataFrame(summed)

    fig, ax = plt.subplots()

    ax.bar(df.index, df.distance)
    ax.set_xticks(df.index)
    ax.xaxis.set_major_formatter(mdates.DateFormatter("%B %d"))
    ax.xaxis.set_minor_formatter(mdates.DateFormatter("%B %d"))
    plt.xticks(rotation=90)

    fig = ax.get_figure()

    fig.set_figheight(10)
    fig.set_figwidth(12)

    plt.title('2019 Weekly Running Miles')
    plt.ylabel('Distance /m')

    fig.savefig("output.png")


    I tried changing it like this:

    ax.bar(df.index, df.distance,width=1)

    0.9 does not look any different and 1.0 looks like this:

    [​IMG]

    Continue reading...

Compartilhe esta Página