Change y range to start from 0 with matplotlib

前端 未结 3 959
春和景丽
春和景丽 2020-12-08 09:43

I am using matplotlib to plot data. Here\'s a code that does something similar:

import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
yda         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 10:09

    Try this

    import matplotlib.pyplot as plt
    xdata = [1, 4, 8]
    ydata = [10, 20, 30]
    plt.plot(xdata, ydata)
    plt.ylim(ymin=0)  # this line
    plt.show()
    

    doc string as following:

    >>> help(plt.ylim)
    Help on function ylim in module matplotlib.pyplot:
    
    ylim(*args, **kwargs)
        Get or set the *y*-limits of the current axes.
    
        ::
    
          ymin, ymax = ylim()   # return the current ylim
          ylim( (ymin, ymax) )  # set the ylim to ymin, ymax
          ylim( ymin, ymax )    # set the ylim to ymin, ymax
    
        If you do not specify args, you can pass the *ymin* and *ymax* as
        kwargs, e.g.::
    
          ylim(ymax=3) # adjust the max leaving min unchanged
          ylim(ymin=1) # adjust the min leaving max unchanged
    
        Setting limits turns autoscaling off for the y-axis.
    
        The new axis limits are returned as a length 2 tuple.
    

提交回复
热议问题