pythonmatplotlibvisual-studio-codejupytersavefig

Black background behind a figure's labels and ticks after saving figure, but not in Interactive view


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?


Solution

  • 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