Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot without extending axis limits?

Tags:

matplotlib

I'm trying to draw on an existing axis without extending or modifying its limits.

For example:

import numpy as np
import matplotlib.pyplot as plt

xy = np.random.randn(100, 2)

plt.scatter(xy[:,0], xy[:,1])

Makes a fine plot with well-fitting axis limits.

However, when I try to draw a line on top of it:

xlim = plt.gca().get_xlim()
plt.plot(xlim, xlim, 'k--')

the axis limits are extended, presumably to create padding around the new data.

How can I draw a line without this padding?

like image 822
shadowtalker Avatar asked Oct 10 '18 14:10

shadowtalker


People also ask

How do you set limits on the y-axis?

ylim( limits ) sets the y-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [ymin ymax] , where ymax is greater than ymin .

What are the two ways to adjust axis limits of the plot using MatPlotLib?

To plot the graph, use the plot() function. To set the limit of the x-axis, use the xlim() function. To set the limit of the y-axis, use the ylim() function.

How do you change the axis size on a plot?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions.

How do I change the limit of a plot in Matlab?

Change Axis LimitsCreate a line plot. Specify the axis limits using the xlim and ylim functions. For 3-D plots, use the zlim function. Pass the functions a two-element vector of the form [min max] .


1 Answers

Setting plt.autoscale(False) prevents autoscaling from happening.

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

xy = np.random.randn(100, 2)
# By default plots are autoscaled. 
plt.scatter(xy[:,0], xy[:,1])

#Turn autoscaling off
plt.autoscale(False)
xlim = plt.gca().get_xlim()
plt.plot(xlim, xlim, 'k--')

plt.show()

enter image description here

like image 128
ImportanceOfBeingErnest Avatar answered Sep 28 '22 20:09

ImportanceOfBeingErnest