setting y-axis limit in matplotlib

后端 未结 9 1721
自闭症患者
自闭症患者 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:35

    This worked at least in matplotlib version 2.2.2:

    plt.axis([None, None, 0, 100])
    

    Probably this is a nice way to set up for example xmin and ymax only, etc.

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

    If an axes (generated by code below the code shown in the question) is sharing the range with the first axes, make sure that you set the range after the last plot of that axes.

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

    To add to @Hima's answer, if you want to modify a current x or y limit you could use the following.

    import numpy as np # you probably alredy do this so no extra overhead
    fig, axes = plt.subplot()
    axes.plot(data[:,0], data[:,1])
    xlim = axes.get_xlim()
    # example of how to zoomout by a factor of 0.1
    factor = 0.1 
    new_xlim = (xlim[0] + xlim[1])/2 + np.array((-0.5, 0.5)) * (xlim[1] - xlim[0]) * (1 + factor) 
    axes.set_xlim(new_xlim)
    

    I find this particularly useful when I want to zoom out or zoom in just a little from the default plot settings.

    0 讨论(0)
提交回复
热议问题