matplotlib plot to fill figure only with data poin

2019-09-19 12:07发布

问题:

I am after an extreme form of matplotlib's tight layout. I would like the data points to fill the figure from edge to edge without leaving any borders and without titles, axes, ticks, labels or any other decorations. I want to do something like what figimage does, but for plots instead of raw images.

How do I do that in matplotlib?

回答1:

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