[Python] How to reduce horizontal padding in this matplotlib plot?

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to eliminate horizontal padding of a Matplotlib plot. Here is the script:

    import numpy as np, matplotlib.pyplot as plt

    a = np.random.rand(100)
    plt.figure(figsize=(12, 6), dpi=100)
    plt.tight_layout()
    plt.plot(a)
    plt.show()


    The result contains a lot of horizontal padding:

    [​IMG]

    I tried this alternative, but the result was the same:

    import numpy as np, matplotlib.pyplot as plt

    a = np.random.rand(100)
    fig, ax = plt.subplots()
    fig.set_dpi(100)
    fig.set_size_inches(12, 6)
    ax.autoscale_view('tight')
    ax.plot(a)
    plt.show()


    Is there a simple solution to this problem?

    Continue reading...

Compartilhe esta Página