Skip to content

Commit b273151

Browse files
committed
savefig: make the "transparent" kwarg work as advertised
svn path=/trunk/matplotlib/; revision=8282
1 parent 67e726b commit b273151

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ class TimerBase(object):
868868
Backends need to implement a few specific methods in order to use their
869869
own timing mechanisms so that the timer events are integrated into their
870870
event loops.
871-
871+
872872
Mandatory functions that must be implemented:
873873
* _timer_start: Contains backend-specific code for starting the timer
874874
* _timer_stop: Contains backend-specific code for stopping the timer
@@ -883,7 +883,7 @@ class itself will store the flag and the _on_timer method should
883883
* _on_timer: This is the internal function that any timer object should
884884
call, which will handle the task of running all callbacks that have
885885
been set.
886-
886+
887887
Attributes:
888888
* interval: The time between timer events in milliseconds. Default
889889
is 1000 ms.
@@ -1938,9 +1938,9 @@ def new_timer(self, *args, **kwargs):
19381938
Creates a new backend-specific subclass of :class:`backend_bases.Timer`.
19391939
This is useful for getting periodic events through the backend's native
19401940
event loop. Implemented only for backends with GUIs.
1941-
1941+
19421942
optional arguments:
1943-
1943+
19441944
*interval*
19451945
Timer interval in milliseconds
19461946
*callbacks*

lib/matplotlib/figure.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,10 @@ def savefig(self, *args, **kwargs):
10401040
backend. Most backends support png, pdf, ps, eps and svg.
10411041
10421042
*transparent*:
1043-
If *True*, the figure patch and axes patches will all be
1044-
transparent. This is useful, for example, for displaying
1043+
If *True*, the axes patches will all be transparent; the
1044+
figure patch will also be transparent unless facecolor
1045+
and/or edgecolor are specified via kwargs.
1046+
This is useful, for example, for displaying
10451047
a plot on top of a colored background on a web page. The
10461048
transparency of these patches will be restored to their
10471049
original values upon exit of this function.
@@ -1061,9 +1063,7 @@ def savefig(self, *args, **kwargs):
10611063
10621064
"""
10631065

1064-
for key in ('dpi', 'facecolor', 'edgecolor'):
1065-
if key not in kwargs:
1066-
kwargs[key] = rcParams['savefig.%s'%key]
1066+
kwargs.setdefault('dpi', rcParams['savefig.dpi'])
10671067

10681068
extension = rcParams['savefig.extension']
10691069
if args and is_string_like(args[0]) and '.' not in args[0] and extension != 'auto':
@@ -1072,20 +1072,25 @@ def savefig(self, *args, **kwargs):
10721072

10731073
transparent = kwargs.pop('transparent', False)
10741074
if transparent:
1075-
original_figure_alpha = self.patch.get_alpha()
1076-
self.patch.set_alpha(0.0)
1077-
original_axes_alpha = []
1075+
kwargs.setdefault('facecolor', 'none')
1076+
kwargs.setdefault('edgecolor', 'none')
1077+
original_axes_colors = []
10781078
for ax in self.axes:
10791079
patch = ax.patch
1080-
original_axes_alpha.append(patch.get_alpha())
1081-
patch.set_alpha(0.0)
1080+
original_axes_colors.append((patch.get_facecolor(),
1081+
patch.get_edgecolor()))
1082+
patch.set_facecolor('none')
1083+
patch.set_edgecolor('none')
1084+
else:
1085+
kwargs.setdefault('facecolor', rcParams['savefig.facecolor'])
1086+
kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
10821087

10831088
self.canvas.print_figure(*args, **kwargs)
10841089

10851090
if transparent:
1086-
self.patch.set_alpha(original_figure_alpha)
1087-
for ax, alpha in zip(self.axes, original_axes_alpha):
1088-
ax.patch.set_alpha(alpha)
1091+
for ax, cc in zip(self.axes, original_axes_colors):
1092+
ax.patch.set_facecolor(cc[0])
1093+
ax.patch.set_edgecolor(cc[1])
10891094

10901095
@docstring.dedent_interpd
10911096
def colorbar(self, mappable, cax=None, ax=None, **kw):

0 commit comments

Comments
 (0)