reox
reox

Reputation: 5217

matplotlib: equal axes and 0/0 at lower left corner

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

origin is in lower left corner

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

origin not at left lower corner

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

Answers (1)

devanshshukla99
devanshshukla99

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

Related Questions