Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

python - How to change the range of the x-axis and y-axis in matlibplot?

I am new to matlibplot and I'm trying to draw a circle with radius 1 but have both my x and y axis go from 0 to 3 with an increment of 0.25. Right now, I have drawn the graph and the circle but my x and y axis only go from 0 to 1, so there is little room left either above, below, to the left, or to the right of the circle. Here's the code so far:

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy, pylab

    plt.axes()
    circle=plt.Circle((0, 0), radius=1, fc='w')
    plt.gca().add_patch(circle)
    plt.yticks(np.arange(0, 3, 0.25))
    plt.xticks(np.arange(0, 3, 0.25))
    plt.axis('scaled')
    plt.show()

I've looked at the following questions, but found them either to be a little too advanced for what I'm trying to accomplish or just a tad bit off-topic:

    http://stackoverflow.com/questions/27170139/how-to-change-the-range-of-my-x-axis-in-matplotlib

    http://stackoverflow.com/questions/22642511/change-y-range-to-start-from-0-with-matplotlib

   http://stackoverflow.com/questions/27456185/scaling-axis-for-a-scatter-plot-in-matlibplot-in-python

   http://stackoverflow.com/questions/22642511/change-y-range-to-start-from-0-with-matplotlib

What I want to do now is, while keeping my circle in the same place on the graph, increase the range of my x and y axis from 0-1 to 0-3 while keeping the increment of 0.25 on each axis, allowing me to plot points all around the edge of the circle without having to worry about the top, bottom, or either side of the circle touching either of the two axis. I've looked through the matlibplot documentation, but can't seem to find a simple step-by-step explanation of how to change the spacing on my axis. Any insight on this would be brilliant! Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To change the axes range, you can use

plt.xlim([-3, 3])
plt.ylim([-3, 3])

You will then have to remove the line plt.axis('scaled') for this to work.

import numpy as np
import matplotlib.pyplot as plt
import scipy, pylab

plt.axes()
circle=plt.Circle((0, 0), radius=1, fc='w')
plt.gca().add_patch(circle)
plt.xlim([-3, 3])
plt.ylim([-3, 3])
plt.yticks(np.arange(-3, 3, 0.25))
plt.xticks(np.arange(-3, 3, 0.25))
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...