Black background behind a figure's labels and ticks, only after saving figure but not in Python Interactive view (VS Code with Jupyter functionality)?

Question:

I have a strange problem where if I save a figure, its labels and ticks will have a black background, see this example:

plt.savefig("asdsadsad.png")

enter image description here

I’m not even including any code here because this happens on the simplest plotting, even with code that I made earlier with a different computer that never had this problem. I’m using VS Code with Jupyter functionality and the figures look normal in the Python Interactive view, but have the black border when saved.

Any ideas what could cause this strange problem?

Asked By: KMFR

||

Answers:

plt.savefig will not use the same settings that you plotted with. That’s why the saved image might look different from what you have plotted in Python. To define the background color of your figure, you need to define the facecolor parameter when you call savefig.

plt.savefig('asdsadsad.png', facecolor='w')

Your default facecolor may be set to black in your rcParams

Answered By: busybear

I think I got the same issue, besides having facecolor and edgecolor set to white (‘w’). For some strange reason the problems occured only with PNG image format and shifting to JPG solved everything.

Answered By: Robson F. Vieira