4

There is no shortage of SO questions about saving a matplotlib figure to a file. This is easy using fig.savefig(filename).

But what if I want to save only the plot area itself, not the x- and y-axes, the outline of the plot or the title etc.

For example, I would like to export only the area shown in red below to either a .pdf or a .png: Plot area

Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
  • 1
    could you not just set `fig.subplots_adjust(left=0,right=1,bottom=0,top=1)` before you savefig? then you will lose all the titles, labels, etc. – tmdavison Dec 01 '15 at 18:31

1 Answers1

1

you can do it by simply adding the following lines in your code

plt.axis('off')
plt.savefig("test.png",bbox_inches='tight')

plt.axis('off') hides your axis and bbox_inches='tight' crops the picture of the white spaces where the axis were.

Pear666
  • 53
  • 9
  • This is pretty good. Is there a way to do `plt.axis('off`)` without using the `pyplot` object? I tend to work with `ax` and `fig` objects. (I do `fig.tight_layout()` for the `bbox_inches` thing. – LondonRob Dec 02 '15 at 16:44