Matplotlib/pyplot: How to enforce axis range?

前端 未结 4 1690
时光取名叫无心
时光取名叫无心 2020-12-14 05:40

I would like to draw a standard 2D line graph with pylot, but force the axes\' values to be between 0 and 600 on the x, and 10k and 20k on the y. Let me go with an example.

4条回答
  •  眼角桃花
    2020-12-14 06:17

    I tried all of those above answers, and I then summarized a pipeline of how to draw the fixed-axes image. It applied both to show function and savefig function.

    1. before you plot:

      fig = pylab.figure()
      ax = fig.gca()
      ax.set_autoscale_on(False)
      

    This is to request an ax which is subplot(1,1,1).

    1. During the plot:

      ax.plot('You plot argument') # Put inside your argument, like ax.plot(x,y,label='test')
      ax.axis('The list of range') # Put in side your range [xmin,xmax,ymin,ymax], like ax.axis([-5,5,-5,200])
      
    2. After the plot:

      1. To show the image :

        fig.show()
        
      2. To save the figure :

        fig.savefig('the name of your figure')
        

    I find out that put axis at the front of the code won't work even though I have set autoscale_on to False.

    I used this code to create a series of animation. And below is the example of combing multiple fixed axes images into an animation. img

提交回复
热议问题