8000 Style cleanup to pyplot. · matplotlib/matplotlib@2f5a48a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f5a48a

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 fd40d7d commit 2f5a48a

File tree

1 file changed

+50
-75
lines changed

1 file changed

+50
-75
lines changed

lib/matplotlib/pyplot.py

Lines changed: 50 additions & 75 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 pylab_setup, _get_running_interactive_framework
7170

7271
_log = logging.getLogger(__name__)
@@ -480,88 +479,65 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
480479
in the matplotlibrc file.
481480
"""
482481

483-
if figsize is None:
484-
figsize = rcParams['figure.figsize']
485-
if dpi is None:
486-
dpi = rcParams['figure.dpi']
487-
if facecolor is None:
488-
facecolor = rcParams['figure.facecolor']
489-
if edgecolor is None:
490-
edgecolor = rcParams['figure.edgecolor']
491-
492482
allnums = get_fignums()
493483
next_num = max(allnums) + 1 if allnums else 1
494-
figLabel = ''
484+
fig_label = ''
495485
if num is None:
496486
num = next_num
497487
elif isinstance(num, str):
498-
figLabel = num
499-
allLabels = get_figlabels()
500-
if figLabel not in allLabels:
501-
if figLabel == 'all':
488+
fig_label = num
489+
all_labels = get_figlabels()
490+
if fig_label not in all_labels:
491+
if fig_label == 'all':
502492
cbook._warn_external(
503-
"close('all') closes all existing figures")
493+
"close('all') closes all existing figures.")
504494
num = next_num
505495
else:
506-
inum = allLabels.index(figLabel)
496+
inum = all_labels.index(fig_label)
507497
num = allnums[inum]
508498
else:
509499
num = int(num) # crude validation of num argument
510500

511-
figManager = _pylab_helpers.Gcf.get_fig_manager(num)
512-
if figManager is None:
501+
manager = _pylab_helpers.Gcf.get_fig_manager(num)
502+
if manager is None:
513503
max_open_warning = rcParams['figure.max_open_warning']
514-
515504
if len(allnums) >= max_open_warning >= 1:
516505
cbook._warn_external(
517-
"More than %d figures have been opened. Figures "
518-
"created through the pyplot interface "
519-
"(`matplotlib.pyplot.figure`) are retained until "
520-
"explicitly closed and may consume too much memory. "
521-
"(To control this warning, see the rcParam "
522-
"`figure.max_open_warning`)." %
523-
max_open_warning, RuntimeWarning)
524-
506+
"More than %d figures have been opened. Figures created "
507+
"through the pyplot interface (`matplotlib.pyplot.figure`) "
508+
"are retained until explicitly closed and may consume too "
509+
"much memory. (To control this warning, see the rcParam "
510+
"`figure.max_open_warning`)." % max_open_warning,
511+
RuntimeWarning)
525512
if get_backend().lower() == 'ps':
526513
dpi = 72
527-
528-
figManager = new_figure_manager(num, figsize=figsize,
529-
dpi=dpi,
530-
facecolor=facecolor,
531-
edgecolor=edgecolor,
532-
frameon=frameon,
533-
FigureClass=FigureClass,
534-
**kwargs)
535-
536-
if figLabel:
537-
figManager.set_window_title(figLabel)
538-
figManager.canvas.figure.set_label(figLabel)
539-
514+
manager = new_figure_manager(
515+
num, figsize=figsize, dpi=dpi,
516+
facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
517+
FigureClass=FigureClass, **kwargs)
518+
if fig_label:
519+
manager.set_window_title(fig_label)
520+
manager.canvas.figure.set_label(fig_label)
540521
# make this figure current on button press event
541-
def make_active(event):
542-
_pylab_helpers.Gcf.set_active(figManager)
543-
544-
cid = figManager.canvas.mpl_connect('button_press_event', make_active)
545-
figManager._cidgcf = cid
546-
547-
_pylab_helpers.Gcf.set_active(figManager)
548-
fig = figManager.canvas.figure
522+
manager._cidgcf = manager.canvas.mpl_connect(
523+
'button_press_event',
524+
lambda event: _pylab_helpers.Gcf.set_active(manager))
525+
_pylab_helpers.Gcf.set_active(manager)
526+
fig = manager.canvas.figure
549527
fig.number = num
550-
551528
# make sure backends (inline) that we don't ship that expect this
552529
# to be called in plotting commands to make the figure call show
553530
# still work. There is probably a better way to do this in the
554531
# FigureManager base class.
555532
if matplotlib.is_interactive():
556533
draw_if_interactive()
557-
558534
if _INSTALL_FIG_OBSERVER:
559535
fig.stale_callback = _auto_draw_if_interactive
560536

561537
if clear:
562-
figManager.canvas.figure.clear()
538+
manager.canvas.figure.clear()
563539

564-
return figManager.canvas.figure
540+
return manager.canvas.figure
565541

566542

567543
def _auto_draw_if_interactive(fig, val):
@@ -580,9 +556,9 @@ def _auto_draw_if_interactive(fig, val):
580556

581557
def gcf():
582558
"""Get a reference to the current figure."""
583-
figManager = _pylab_helpers.Gcf.get_active()
584-
if figManager is not None:
585-
return figManager.canvas.figure
559+
manager = _pylab_helpers.Gcf.get_active()
560+
if manager is not None:
561+
return manager.canvas.figure
586562
else:
587563
return figure()
588564

@@ -599,9 +575,9 @@ def get_fignums():
599575

600576
def get_figlabels():
601577
"""Return a list of existing figure labels."""
602-
figManagers = _pylab_helpers.Gcf.get_all_fig_managers()
603-
figManagers.sort(key=lambda m: m.num)
604-
return [m.canvas.figure.get_label() for m in figManagers]
578+
managers = _pylab_helpers.Gcf.get_all_fig_managers()
579+
managers.sort(key=lambda m: m.num)
580+
return [m.canvas.figure.get_label() for m in managers]
605581

606582

607583
def get_current_fig_manager():
@@ -610,11 +586,10 @@ def get_current_fig_manager():
610586
611587
If there is currently no active figure, a new one is created.
612588
"""
613-
figManager = _pylab_helpers.Gcf.get_active()
614-
if figManager is None:
615-
gcf() # creates an active figure as a side effect
616-
figManager = _pylab_helpers.Gcf.get_active()
617-
return figManager
589+
return (_pylab_helpers.Gcf.get_active()
590+
# gcf() creates an active figure as a side effect, if get_active()
591+
# returns None.
592+
or gcf().canvas.manager)
618593

619594

620595
@docstring.copy(FigureCanvasBase.mpl_connect)
@@ -644,11 +619,11 @@ def close(fig=None):
644619
645620
"""
646621
if fig is None:
647-
figManager = _pylab_helpers.Gcf.get_active()
648-
if figManager is None:
622+
manager = _pylab_helpers.Gcf.get_active()
623+
if manager is None:
649624
return
650625
else:
651-
_pylab_helpers.Gcf.destroy(figManager.num)
626+
_pylab_helpers.Gcf.destroy(manager.num)
652627
elif fig == 'all':
653628
_pylab_helpers.Gcf.destroy_all()
654629
elif isinstance(fig, int):
@@ -658,9 +633,9 @@ def close(fig=None):
658633
# can use its integer representation
659634
_pylab_helpers.Gcf.destroy(fig.int)
660635
elif isinstance(fig, str):
661-
allLabels = get_figlabels()
662-
if fig in allLabels:
663-
num = get_fignums()[allLabels.index(fig)]
636+
all_labels = get_figlabels()
637+
if fig in all_labels:
638+
num = get_fignums()[all_labels.index(fig)]
664639
_pylab_helpers.Gcf.destroy(num)
665640
elif isinstance(fig, Figure):
666641
_pylab_helpers.Gcf.destroy_fig(fig)

0 commit comments

Comments
 (0)
0