Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the plot with the labels and ticks area transparent but not the main plot area in matplotlib?

I'm trying to save a plot with the labels area (the area where you print the xlabel, ylabel and title) transparent and the main plot (the area where you plot the data) not transparent.

enter image description here

I'm using the transparent command in savefig:

plt.savefig("name",tranparent=True)

The problem is that this makes all the figure transparent.

In the documentation says this about the transparent argument:

transparent : bool

If True, the axes patches will all be transparent; the figure patch will also be transparent unless facecolor and/or edgecolor are specified via kwargs. This is useful, for example, for displaying a plot on top of a colored background on a web page. The transparency of these patches will be restored to their original values upon exit of this function.

So I tried changing the facecolor and edgecolor arguments, but I can't find a combination that gives the plot that I want.

Help.

like image 245
Claudio Pierard Avatar asked Apr 06 '18 04:04

Claudio Pierard


People also ask

How do I make Matplotlib transparent?

If you want to make something completely transparent, simply set the alpha value of the corresponding color to 0. For plt. savefig , there is also a "lazy" option by setting the rc-parameter savefig. transparent to True , which sets the alpha of all facecolors to 0%.

How do I stop Matplotlib overlapping?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

How do you avoid overlapping subplots in Python?

Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.


1 Answers

There is a bug in the newest version of matplotlib. I'm not sure if this applies to here, because you do not state which version you are using.

The workaround would be to use a transparent facecolor and leave the other arguments at their defaults.

fig.savefig("fname", facecolor=(1,1,1,0))
like image 72
ImportanceOfBeingErnest Avatar answered Sep 29 '22 21:09

ImportanceOfBeingErnest