[Python] Wrong x axis limits on matplotlib.scatter

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

  1. Stack

    Stack Membro Participativo

    I'm plotting three datasets using matplotlib.pyplot.scatter. Each dataset is loaded into a pandas DataFrame and contains Time and Delay_ms columns. I normalize the Time column to start from 0 by subtracting the minimum, so all datasets should start at 0 and end near their respective durations (545s, 542s, 577s respectively). However, on the plot, the x-axis limits appear inconsistent between datasets, even though their durations are similar.

    Key points:

    • Datasets although with similar duration have different row densities (with respect to time)
    • df0 has ~3.2 million rows
    • df1 has ~3.5 million rows
    • df2 has ~88k rows
    • All durations are roughly in the same range (meaning min to max Time)
    • x (Time) values have very small diffrences:

    Time Sched_delay_ms
    0 0.0000000000 0.4190000000
    1 0.0013510000 0.0000000000
    2 0.0014590000 1.5880000000
    3 0.0015320000 0.0000000000
    4 0.0029610000 0.0000000000

    • This issue also appears when plotting each dataset individually
    • Could scatter be dropping or skipping points silently?

    Here's the relevant code:

    plt.figure(figsize=(15, 8), dpi=300)
    for (df, label) in datasets:
    df['Time'] = df['Time'] - df['Time'].min() # Normalize time
    df.sort_values(by='Time', inplace=True)
    avg_delay = df['Delay_ms'].mean()
    scatter = plt.scatter(df['Time'], df['Delay_ms'], label=label, alpha=0.6, s=4)
    plt.axhline(y=avg_delay, linestyle='--', alpha=0.8,
    color=scatter.get_facecolor()[0], label=f'{label} avg ({avg_delay:.2f} ms)')

    plt.show()

    Continue reading...

Compartilhe esta Página