Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 833
busebd12 Avatar asked Mar 30 '15 01:03

busebd12


People also ask

How do you set the range of the X-axis and Y-axis in Python?

To set range of x-axis and y-axis, use xlim() and ylim() function respectively. To add a title to the plot, use the title() function. To add label at axes, use xlabel() and ylabel() functions. To visualize the plot, use the show() function.

How do you set a range on the y-axis?

The ylim() function is used to set or to get the y-axis limits or we can say y-axis range. By default, matplotlib automatically chooses the range of y-axis limits to plot the data on the graph area. But if we want to change that range of the current axes then we can use the ylim() function.

How do I change the y-axis values in MatPlotLib?

To specify the value of axes, create a list of characters. Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively. Plot the line using x and y, color=red, using plot() method. Make x and y margin 0.


2 Answers

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()
like image 97
Julien Spronck Avatar answered Sep 17 '22 13:09

Julien Spronck


Use first xticks and yticks before xlim and ylim. This will create an array before it sets the limit

like image 22
Simon Luzara Avatar answered Sep 18 '22 13:09

Simon Luzara