While a solution may be found taking the bits and pieces from an answer to this question it may not be obvious at first sight.
The main idea to have no padding around the axes, is to make the axes the same size as the figure.
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
Alternatively, one can set the outer spacings to 0
.
fig, ax = plt.subplots()
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
Then, in order to remove the axis decorations one can use
ax.set_axis_off()
or
ax.axis("off")
This may now still leave some space between the plotted line and the edge. This can be removed by setting the limits appropriately using ax.set_xlim()
and ax.set_ylim()
. Or, by using ax.margins(0)
A complete example may thus look like
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis("off")
ax.plot([2,3,1])
ax.margins(0)
plt.show()
