Skip to content

Style cleanup to pyplot. #13569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2020
Merged
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
88 changes: 37 additions & 51 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,63 +622,49 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
in the matplotlibrc file.
"""

if figsize is None:
figsize = rcParams['figure.figsize']
if dpi is None:
dpi = rcParams['figure.dpi']
if facecolor is None:
facecolor = rcParams['figure.facecolor']
if edgecolor is None:
edgecolor = rcParams['figure.edgecolor']

allnums = get_fignums()
next_num = max(allnums) + 1 if allnums else 1
figLabel = ''
fig_label = ''
if num is None:
num = next_num
elif isinstance(num, str):
figLabel = num
allLabels = get_figlabels()
if figLabel not in allLabels:
if figLabel == 'all':
fig_label = num
all_labels = get_figlabels()
if fig_label not in all_labels:
if fig_label == 'all':
cbook._warn_external(
"close('all') closes all existing figures")
"close('all') closes all existing figures.")
num = next_num
else:
inum = allLabels.index(figLabel)
inum = all_labels.index(fig_label)
num = allnums[inum]
else:
num = int(num) # crude validation of num argument

figManager = _pylab_helpers.Gcf.get_fig_manager(num)
if figManager is None:
manager = _pylab_helpers.Gcf.get_fig_manager(num)
if manager is None:
max_open_warning = rcParams['figure.max_open_warning']

if len(allnums) == max_open_warning >= 1:
cbook._warn_external(
"More than %d figures have been opened. Figures "
"created through the pyplot interface "
"(`matplotlib.pyplot.figure`) are retained until "
"explicitly closed and may consume too much memory. "
"(To control this warning, see the rcParam "
"`figure.max_open_warning`)." %
max_open_warning, RuntimeWarning)
f"More than {max_open_warning} figures have been opened. "
f"Figures created through the pyplot interface "
f"(`matplotlib.pyplot.figure`) are retained until explicitly "
f"closed and may consume too much memory. (To control this "
f"warning, see the rcParam `figure.max_open_warning`).",
RuntimeWarning)

if get_backend().lower() == 'ps':
dpi = 72

figManager = new_figure_manager(num, figsize=figsize,
dpi=dpi,
facecolor=facecolor,
edgecolor=edgecolor,
frameon=frameon,
FigureClass=FigureClass,
**kwargs)
fig = figManager.canvas.figure
if figLabel:
fig.set_label(figLabel)
manager = new_figure_manager(
num, figsize=figsize, dpi=dpi,
facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
FigureClass=FigureClass, **kwargs)
fig = manager.canvas.figure
if fig_label:
fig.set_label(fig_label)

_pylab_helpers.Gcf._set_new_active_manager(figManager)
_pylab_helpers.Gcf._set_new_active_manager(manager)

# make sure backends (inline) that we don't ship that expect this
# to be called in plotting commands to make the figure call show
Expand All @@ -690,9 +676,9 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
fig.stale_callback = _auto_draw_if_interactive

if clear:
figManager.canvas.figure.clear()
manager.canvas.figure.clear()

return figManager.canvas.figure
return manager.canvas.figure


def _auto_draw_if_interactive(fig, val):
Expand Down Expand Up @@ -723,9 +709,9 @@ def gcf():
If no current figure exists, a new one is created using
`~.pyplot.figure()`.
"""
figManager = _pylab_helpers.Gcf.get_active()
if figManager is not None:
return figManager.canvas.figure
manager = _pylab_helpers.Gcf.get_active()
if manager is not None:
return manager.canvas.figure
else:
return figure()

Expand All @@ -742,9 +728,9 @@ def get_fignums():

def get_figlabels():
"""Return a list of existing figure labels."""
figManagers = _pylab_helpers.Gcf.get_all_fig_managers()
figManagers.sort(key=lambda m: m.num)
return [m.canvas.figure.get_label() for m in figManagers]
managers = _pylab_helpers.Gcf.get_all_fig_managers()
managers.sort(key=lambda m: m.num)
return [m.canvas.figure.get_label() for m in managers]


def get_current_fig_manager():
Expand Down Expand Up @@ -791,11 +777,11 @@ def close(fig=None):

"""
if fig is None:
figManager = _pylab_helpers.Gcf.get_active()
if figManager is None:
manager = _pylab_helpers.Gcf.get_active()
if manager is None:
return
else:
_pylab_helpers.Gcf.destroy(figManager)
_pylab_helpers.Gcf.destroy(manager)
elif fig == 'all':
_pylab_helpers.Gcf.destroy_all()
elif isinstance(fig, int):
Expand All @@ -805,9 +791,9 @@ def close(fig=None):
# can use its integer representation
_pylab_helpers.Gcf.destroy(fig.int)
elif isinstance(fig, str):
allLabels = get_figlabels()
if fig in allLabels:
num = get_fignums()[allLabels.index(fig)]
all_labels = get_figlabels()
if fig in all_labels:
num = get_fignums()[all_labels.index(fig)]
_pylab_helpers.Gcf.destroy(num)
elif isinstance(fig, Figure):
_pylab_helpers.Gcf.destroy_fig(fig)
Expand Down