
Set the axes limits in Matplotlib
The scatter()
draws points in Matplotlib.
import numpy as np
from matplotlib import pyplot
a = np.array([1, 0])
b = np.array([3, 4])
x = np.array([a[0], b[0]])
y = np.array([a[1], b[1]])
pyplot.scatter(x, y)
pyplot.savefig('axes_auto.jpg')

The range of x, y axes are set automatically and many points may by placed just on the axes. For avoiding that, Matplotlib has the set_xlim()
and set_ylim()
method.
Set the axes limits
import numpy as np
from matplotlib import pyplot
a = np.array([1, 0])
b = np.array([3, 4])
x = np.array([a[0], b[0]])
y = np.array([a[1], b[1]])
axes = pyplot.gca()
axes.set_xlim([-6, 6])
axes.set_ylim([-6, 6])
pyplot.scatter(x, y)
pyplot.savefig('axis_range.jpg')

You can set the axes limits in the figure by set_xlim()
and set_ylim()
but the aspect ratio is not 1:1 in the above figure.
Set aspect
So, when using set_xlim()
and set_ylim()
methods, set_aspect()
should be used to set the aspect ratio of 1:1.
import numpy as np
from matplotlib import pyplot
a = np.array([1, 0])
b = np.array([3, 4])
x = np.array([a[0], b[0]])
y = np.array([a[1], b[1]])
axes = pyplot.gca()
axes.set_xlim([-6, 6])
axes.set_ylim([-6, 6])
axes.set_aspect(1)
pyplot.scatter(x, y)
pyplot.savefig('axis_aspect.jpg')

Comments
Powered by Markdown