[Python] Set margins of a time series plotted with pandas

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

  1. Stack

    Stack Membro Participativo

    I have the following code for generating a time series plot

    import numpy as np
    fig = plt.figure()
    ax = fig.add_subplot(111)
    series = pd.Series([np.sin(ii*np.pi) for ii in range(30)],
    index=pd.date_range(start='2019-01-01', end='2019-12-31',
    periods=30))
    series.plot(ax=ax)


    [​IMG]

    I want to set an automatic limit for x and y, I tried using ax.margins() but it does not seem to work:

    ax.margins(y=0.1, x=0.05)
    # even with
    # ax.margins(y=0.1, x=5)


    [​IMG]

    What I am looking for is an automatic method like padding=0.1 (10% of whitespace around the graph)

    Continue reading...

Compartilhe esta Página