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")
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?
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’.
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!
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With