Python – How to change the range of the x-axis and y-axis in matlibplot

matplotlibpython

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!

Best Answer

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()