Reputation: 5217
I would like to plot some data in an equal-spaced plot like this:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10)
y = 2 * x
plt.figure(figsize=(2, 3), dpi=100)
plt.scatter(x, y)
plt.axline((0,0), slope=1, linestyle=':')
plt.gca().set_aspect('equal', 'datalim')
In this example, the origin (0/0) is in the lower left corner and the 1:1 line passes right through the corner. However, if I change the size of the figure just slightly, either the x or y axis will start on a different location:
plt.figure(figsize=(4, 3), dpi=100)
plt.scatter(x, y)
plt.axline((0,0), slope=1, linestyle=':')
plt.gca().set_aspect('equal', 'datalim')
Is it possible to enforce both an equal spacing on both axes but also keep the origin (in my case always 0/0) in the lower left corner?
Upvotes: 1
Views: 668
Reputation: 36
Setting the limits on the x and y axis using plt.xlim
and plt.ylim
should solve the problem
checkout https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.xlim.html
Upvotes: 0