Automatically Rescale ylim and xlim in Matplotlib

前端 未结 1 1613
无人及你
无人及你 2020-11-29 06:52

I\'m plotting data in Python using matplotlib. I am updating the data of the plot based upon some calculations and want the ylim and xlim to be rescaled automatically. Inste

相关标签:
1条回答
  • 2020-11-29 07:47

    You will need to update the axes' dataLim, then subsequently update the axes' viewLim based on the dataLim. The approrpiate methods are axes.relim() and ax.autoscale_view() method. Your example then looks like:

    import random
    import matplotlib.pyplot as pyplot
    
    pyplot.ion()
    
    x = range(10)
    y = lambda m: [m*random.random() for i in range(10)]
    
    pLine, = pyplot.plot(x, y(1))
    
    for i in range(10):
        pLine.set_ydata(y(i+1))
    
    ax = pyplot.gca()
    
    # recompute the ax.dataLim
    ax.relim()
    # update ax.viewLim using the new dataLim
    ax.autoscale_view()
    pyplot.draw()
    
    0 讨论(0)
提交回复
热议问题