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

1
  • 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. Commented Dec 1, 2015 at 18:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.