Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 346
KMFR Avatar asked Dec 11 '18 15:12

KMFR


People also ask

How to hide the tick labels in a Python plot?

If the background color of the plot is white. By setting the color of tick labels as white we can easily hide tick labels. Basically, it does make the tick labels invisible but set the label color to be white. If the background color is not white, then this method doesn’t work. Use xticks () and yticks () method and pass the color argument as ‘w’.

What is a Matplotlib tick and tick label?

Ticks are the markers denoting data points on the axes and tick labels are the name given to ticks. By default matplotlib itself marks the data points on the axes but it has also provided us with setting their own axes having ticks and tick labels of their choice. Attention geek!

How to set the background color of a plot in Python?

From the below figure one can infer that a plot consists of X-axis, Y-axis, plot title and the axes. By default, the color of the plot is white. If we have to set the background color of the plot so that our plot looks beautiful, we have to make the axes object, by using axes() attribute after plotting the graph.

How do I get Matplotlib to recognize Tkinter GUI?

In order to get matplotlib to recognize the TkInter GUI library, we need to: Step 1: Access our plotting virtual environment via workon plotting . Step 2: Use pip to uninstall matplotlib (since we installed it via pip earlier in this article). Step 3: Pull down matplotlib from the GitHub repo. Step 4: Install matplotlib from source using setup.py .


1 Answers

plt.savefig will overwrite your 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.

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

Your default facecolor may be set to black in your rcParams

like image 143
busybear Avatar answered Oct 13 '22 21:10

busybear