Skip to content

Commit 3bc8636

Browse files
committed
Style cleanup to pyplot.
... preliminary to more refactoring. Nearly everything is mechanical; the only subtle point is that figure() does not need to handle the figure.foo rcParams as the Figure constructor takes care of that.
1 parent 030157c commit 3bc8636

File tree

1 file changed

+46
-70
lines changed

1 file changed

+46
-70
lines changed

lib/matplotlib/pyplot.py

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@
6161
from matplotlib.patches import Polygon, Rectangle, Circle, Arrow
6262
from matplotlib.widgets import SubplotTool, Button, Slider, Widget
6363

64-
from .ticker import TickHelper, Formatter, FixedFormatter, NullFormatter,\
65-
FuncFormatter, FormatStrFormatter, ScalarFormatter,\
66-
LogFormatter, LogFormatterExponent, LogFormatterMathtext,\
67-
Locator, IndexLocator, FixedLocator, NullLocator,\
68-
LinearLocator, LogLocator, AutoLocator, MultipleLocator,\
69-
MaxNLocator
64+
from .ticker import (
65+
TickHelper, Formatter, FixedFormatter, NullFormatter, FuncFormatter,
66+
FormatStrFormatter, ScalarFormatter, LogFormatter, LogFormatterExponent,
67+
LogFormatterMathtext, Locator, IndexLocator, FixedLocator, NullLocator,
68+
LinearLocator, LogLocator, AutoLocator, MultipleLocator, MaxNLocator)
7069
from matplotlib.backends import _get_running_interactive_framework
7170

7271
_log = logging.getLogger(__name__)
@@ -492,88 +491,65 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
492491
in the matplotlibrc file.
493492
"""
494493

495-
if figsize is None:
496-
figsize = rcParams['figure.figsize']
497-
if dpi is None:
498-
dpi = rcParams['figure.dpi']
499-
if facecolor is None:
500-
facecolor = rcParams['figure.facecolor']
501-
if edgecolor is None:
502-
edgecolor = rcParams['figure.edgecolor']
503-
504494
allnums = get_fignums()
505495
next_num = max(allnums) + 1 if allnums else 1
506-
figLabel = ''
496+
fig_label = ''
507497
if num is None:
508498
num = next_num
509499
elif isinstance(num, str):
510-
figLabel = num
511-
allLabels = get_figlabels()
512-
if figLabel not in allLabels:
513-
if figLabel == 'all':
500+
fig_label = num
501+
all_labels = get_figlabels()
502+
if fig_label not in all_labels:
503+
if fig_label == 'all':
514504
cbook._warn_external(
515-
"close('all') closes all existing figures")
505+
"close('all') closes all existing figures.")
516506
num = next_num
517507
else:
518-
inum = allLabels.index(figLabel)
508+
inum = all_labels.index(fig_label)
519509
num = allnums[inum]
520510
else:
521511
num = int(num) # crude validation of num argument
522512

523-
figManager = _pylab_helpers.Gcf.get_fig_manager(num)
524-
if figManager is None:
513+
manager = _pylab_helpers.Gcf.get_fig_manager(num)
514+
if manager is None:
525515
max_open_warning = rcParams['figure.max_open_warning']
526-
527516
if len(allnums) >= max_open_warning >= 1:
528517
cbook._warn_external(
529-
"More than %d figures have been opened. Figures "
530-
"created through the pyplot interface "
531-
"(`matplotlib.pyplot.figure`) are retained until "
532-
"explicitly closed and may consume too much memory. "
533-
"(To control this warning, see the rcParam "
534-
"`figure.max_open_warning`)." %
535-
max_open_warning, RuntimeWarning)
536-
518+
f"More than {max_open_warning} figures have been opened. "
519+
f"Figures created through the pyplot interface "
520+
f"(`matplotlib.pyplot.figure`) are retained until explicitly "
521+
f"closed and may consume too much memory. (To control this "
522+
f"warning, see the rcParam `figure.max_open_warning`).",
523+
RuntimeWarning)
537524
if get_backend().lower() == 'ps':
538525
dpi = 72
539-
540-
figManager = new_figure_manager(num, figsize=figsize,
541-
dpi=dpi,
542-
facecolor=facecolor,
543-
edgecolor=edgecolor,
544-
frameon=frameon,
545-
FigureClass=FigureClass,
546-
**kwargs)
547-
548-
if figLabel:
549-
figManager.set_window_title(figLabel)
550-
figManager.canvas.figure.set_label(figLabel)
551-
526+
manager = new_figure_manager(
527+
num, figsize=figsize, dpi=dpi,
528+
facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
529+
FigureClass=FigureClass, **kwargs)
530+
if fig_label:
531+
manager.set_window_title(fig_label)
532+
manager.canvas.figure.set_label(fig_label)
552533
# make this figure current on button press event
553-
def make_active(event):
554-
_pylab_helpers.Gcf.set_active(figManager)
555-
556-
cid = figManager.canvas.mpl_connect('button_press_event', make_active)
557-
figManager._cidgcf = cid
558-
559-
_pylab_helpers.Gcf.set_active(figManager)
560-
fig = figManager.canvas.figure
534+
manager._cidgcf = manager.canvas.mpl_connect(
535+
'button_press_event',
536+
lambda event: _pylab_helpers.Gcf.set_active(manager))
537+
_pylab_helpers.Gcf.set_active(manager)
538+
fig = manager.canvas.figure
561539
fig.number = num
562-
563540
# make sure backends (inline) that we don't ship that expect this
564541
# to be called in plotting commands to make the figure call show
565542
# still work. There is probably a better way to do this in the
566543
# FigureManager base class.
567544
if matplotlib.is_interactive():
568545
draw_if_interactive()
569-
570546
if _INSTALL_FIG_OBSERVER:
571547
fig.stale_callback = _auto_draw_if_interactive
572548

573549
if clear:
574-
figManager.canvas.figure.clear()
550+
manager.canvas.figure.clear()
575551

576-
return figManager.canvas.figure
552+
return manager.canvas.figure
577553

578554

579555
def _auto_draw_if_interactive(fig, val):
@@ -597,9 +573,9 @@ def gcf():
597573
If no current figure exists, a new one is created using
598574
`~.pyplot.figure()`.
599575
"""
600-
figManager = _pylab_helpers.Gcf.get_active()
601-
if figManager is not None:
602-
return figManager.canvas.figure
576+
manager = _pylab_helpers.Gcf.get_active()
577+
if manager is not None:
578+
return manager.canvas.figure
603579
else:
604580
return figure()
605581

@@ -616,9 +592,9 @@ def get_fignums():
616592

617593
def get_figlabels():
618594
"""Return a list of existing figure labels."""
619-
figManagers = _pylab_helpers.Gcf.get_all_fig_managers()
620-
figManagers.sort(key=lambda m: m.num)
621-
return [m.canvas.figure.get_label() for m in figManagers]
595+
managers = _pylab_helpers.Gcf.get_all_fig_managers()
596+
managers.sort(key=lambda m: m.num)
597+
return [m.canvas.figure.get_label() for m in managers]
622598

623599

624600
def get_current_fig_manager():
@@ -665,11 +641,11 @@ def close(fig=None):
665641
666642
"""
667643
if fig is None:
668-
figManager = _pylab_helpers.Gcf.get_active()
669-
if figManager is None:
644+
manager = _pylab_helpers.Gcf.get_active()
645+
if manager is None:
670646
return
671647
else:
672-
_pylab_helpers.Gcf.destroy(figManager.num)
648+
_pylab_helpers.Gcf.destroy(manager.num)
673649
elif fig == 'all':
674650
_pylab_helpers.Gcf.destroy_all()
675651
elif isinstance(fig, int):
@@ -679,9 +655,9 @@ def close(fig=None):
679655
# can use its integer representation
680656
_pylab_helpers.Gcf.destroy(fig.int)
681657
elif isinstance(fig, str):
682-
allLabels = get_figlabels()
683-
if fig in allLabels:
684-
num = get_fignums()[allLabels.index(fig)]
658+
all_labels = get_figlabels()
659+
if fig in all_labels:
660+
num = get_fignums()[all_labels.index(fig)]
685661
_pylab_helpers.Gcf.destroy(num)
686662
elif isinstance(fig, Figure):
687663
_pylab_helpers.Gcf.destroy_fig(fig)

0 commit comments

Comments
 (0)