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?
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 .
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.
To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions.
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] .
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With