Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/2017-11-12_reset_rcparams_in_clf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rcParams can be reset in clf
----------------------------

``figure.clf()`` can take an optional argument ``reset_rcparams``. If set to ``True``,
unset figure properties will be set to the values specified in rcParams.
39 changes: 34 additions & 5 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def __init__(self,
raise ValueError('figure size must be finite not '
'{}'.format(figsize))
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)
self._figsize = figsize

self.dpi_scale_trans = Affine2D().scale(dpi, dpi)
# do not use property as it will trigger
Expand All @@ -351,6 +352,8 @@ def __init__(self,
facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth)
self._set_artist_props(self.patch)
self.patch.set_aa(False)
self._facecolor = facecolor
self._edgecolor = edgecolor

self._hold = rcParams['axes.hold']
if self._hold is None:
Expand All @@ -364,6 +367,7 @@ def __init__(self,

self.subplotpars = subplotpars
self.set_tight_layout(tight_layout)
self._tight = tight_layout

self._axstack = AxesStack() # track all figure axes and current axes
self.clf()
Expand Down Expand Up @@ -1226,12 +1230,18 @@ def _break_share_link(ax, grouper):
if last_ax is not None:
_reset_loc_form(last_ax.xaxis)

def clf(self, keep_observers=False):
def clf(self, keep_observers=False, reset_rcparams=False):
"""
Clear the figure.

Set *keep_observers* to True if, for example,
a gui widget is tracking the axes in the figure.
Parameters
----------
keep_observers : bool, optional
Set *keep_observers* to True if, for example,
a gui widget is tracking the axes in the figure.
reset_rcparams : bool, optional
Set *reset_rcparams* to True to reset figure
parameters to rcParams values.
"""
self.suppressComposite = None
self.callbacks = cbook.CallbackRegistry()
Expand All @@ -1252,14 +1262,33 @@ def clf(self, keep_observers=False):
self.legends = []
if not keep_observers:
self._axobservers = []
if reset_rcparams:
if self._figsize is None:
self._figsize = rcParams['figure.figsize']
if self._dpi is None:
self._dpi = rcParams['figure.dpi']
if self._facecolor is None:
self._facecolor = rcParams['figure.facecolor']
if self._edgecolor is None:
self._edgecolor = rcParams['figure.edgecolor']
if self.frameon is None:
self.frameon = rcParams['figure.frameon']
self.bbox_inches = Bbox.from_bounds(0, 0, *self._figsize)
self.dpi_scale_trans = Affine2D().scale(self._dpi, self._dpi)
self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans)
self.transFigure = BboxTransformTo(self.bbox)
self.set_facecolor(self._facecolor)
self.set_edgecolor(self._edgecolor)
self.subplotpars.update()
self.set_tight_layout(self._tight)
self._suptitle = None
self.stale = True

def clear(self, keep_observers=False):
def clear(self, keep_observers=False, reset_rcparams=False):
"""
Clear the figure -- synonym for :meth:`clf`.
"""
self.clf(keep_observers=keep_observers)
self.clf(keep_observers=keep_observers, reset_rcparams=reset_rcparams)

@allow_rasterization
def draw(self, renderer):
Expand Down
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from matplotlib.axes import Axes
from matplotlib.ticker import AutoMinorLocator, FixedFormatter
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.dates as mdates
import numpy as np
import warnings
Expand Down Expand Up @@ -68,6 +69,40 @@ def test_clf_keyword():
assert [t.get_text() for t in fig2.texts] == []


def test_reset_rcparams():
# test whether clf resets unset figure properties to rcParams values
fig0 = plt.figure()
fig0._figsize = None
fig0._dpi = None
fig0._facecolor = None
fig0._edgecolor = None
fig0.frameon = None
fig0.clf(reset_rcparams=True)
assert fig0.get_figwidth() == rcParams['figure.figsize'][0]
assert fig0.get_figheight() == rcParams['figure.figsize'][1]
assert fig0.get_dpi() == rcParams['figure.dpi']
assert fig0.get_facecolor() == colors.to_rgba(rcParams['figure.facecolor'])
assert fig0.get_edgecolor() == colors.to_rgba(rcParams['figure.edgecolor'])
assert fig0.get_frameon() == rcParams['figure.frameon']

# set figure properties should not be reset to rcParams
fig1 = plt.figure()
fig1._figsize = (11, 13)
fig1._figsize
fig1._dpi = 251
fig1._facecolor = 'red'
fig1._edgecolor = 'blue'
fig1.clf(reset_rcparams=True)
fig1._figsize
assert not fig1.get_figwidth() == rcParams['figure.figsize'][0]
assert not fig1.get_figheight() == rcParams['figure.figsize'][1]
assert not fig1.get_dpi() == rcParams['figure.dpi']
assert not fig1.get_facecolor() == colors.to_rgba(
rcParams['figure.facecolor'])
assert not fig1.get_edgecolor() == colors.to_rgba(
rcParams['figure.edgecolor'])


@image_comparison(baseline_images=['figure_today'])
def test_figure():
# named figure support
Expand Down