setting y-axis limit in matplotlib

后端 未结 9 1720
自闭症患者
自闭症患者 2020-11-22 15:42

I need help with setting the limits of y-axis on matplotlib. Here is the code that I tried, unsuccessfully.

import matplotlib.pyplot as plt

plt.figure(1, fi         


        
相关标签:
9条回答
  • 2020-11-22 16:19

    This should work. Your code works for me, like for Tamás and Manoj Govindan. It looks like you could try to update Matplotlib. If you can't update Matplotlib (for instance if you have insufficient administrative rights), maybe using a different backend with matplotlib.use() could help.

    0 讨论(0)
  • 2020-11-22 16:25

    Try this . Works for subplots too .

    axes = plt.gca()
    axes.set_xlim([xmin,xmax])
    axes.set_ylim([ymin,ymax])
    
    0 讨论(0)
  • 2020-11-22 16:27

    Your code works also for me. However, another workaround can be to get the plot's axis and then change only the y-values:

    x1,x2,y1,y2 = plt.axis()
    plt.axis((x1,x2,25,250))

    0 讨论(0)
  • 2020-11-22 16:30

    One thing you can do is to set your axis range by yourself by using matplotlib.pyplot.axis.

    matplotlib.pyplot.axis

    from matplotlib import pyplot as plt
    plt.axis([0, 10, 0, 20])
    

    0,10 is for x axis range. 0,20 is for y axis range.

    or you can also use matplotlib.pyplot.xlim or matplotlib.pyplot.ylim

    matplotlib.pyplot.ylim

    plt.ylim(-2, 2)
    plt.xlim(0,10)
    
    0 讨论(0)
  • 2020-11-22 16:33

    Just for fine tuning. If you want to set only one of the boundaries of the axis and let the other boundary unchanged, you can choose one or more of the following statements

    plt.xlim(right=xmax) #xmax is your value
    plt.xlim(left=xmin) #xmin is your value
    plt.ylim(top=ymax) #ymax is your value
    plt.ylim(bottom=ymin) #ymin is your value
    

    Take a look at the documentation for xlim and for ylim

    0 讨论(0)
  • 2020-11-22 16:34

    You can instantiate an object from matplotlib.pyplot.axes and call the set_ylim() on it. It would be something like this:

    import matplotlib.pyplot as plt
    axes = plt.axes()
    axes.set_ylim([0, 1])
    
    0 讨论(0)
提交回复
热议问题