Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aed0979

Browse files
committed
pep8-ify pyplot.
1 parent 2f7aae4 commit aed0979

File tree

2 files changed

+59
-67
lines changed

2 files changed

+59
-67
lines changed

lib/matplotlib/pyplot.py

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
`matplotlib.pyplot` is a state-based interface to matplotlib. It provides
33
a MATLAB-like way of plotting.
44
5-
pyplot is mainly intended for interactive plots and simple cases of programmatic
6-
plot generation::
5+
pyplot is mainly intended for interactive plots and simple cases of
6+
programmatic plot generation::
77
88
import numpy as np
99
import matplotlib.pyplot as plt
@@ -64,7 +64,9 @@
6464
MaxNLocator
6565
from matplotlib.backends import pylab_setup
6666

67+
6768
## Backend detection ##
69+
6870
def _backend_selection():
6971
""" If rcParams['backend_fallback'] is true, check to see if the
7072
current backend is compatible with the current running event
@@ -74,7 +76,7 @@ def _backend_selection():
7476
if not rcParams['backend_fallback'] or backend not in _interactive_bk:
7577
return
7678
is_agg_backend = rcParams['backend'].endswith('Agg')
77-
if 'wx' in sys.modules and not backend in ('WX', 'WXAgg'):
79+
if 'wx' in sys.modules and backend not in ('WX', 'WXAgg'):
7880
import wx
7981
if wx.App.IsMainLoopRunning():
8082
rcParams['backend'] = 'wx' + 'Agg' * is_agg_backend
@@ -224,7 +226,8 @@ def switch_backend(newbackend):
224226
global _backend_mod, new_figure_manager, draw_if_interactive, _show
225227
matplotlib.use(newbackend, warn=False, force=True)
226228
from matplotlib.backends import pylab_setup
227-
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
229+
_backend_mod, new_figure_manager, draw_if_interactive, _show = \
230+
pylab_setup()
228231

229232

230233
def show(*args, **kw):
@@ -284,10 +287,8 @@ def pause(interval):
284287
else:
285288
time.sleep(interval)
286289

287-
288290
## Any Artist ##
289291

290-
291292
def xkcd(scale=1, length=100, randomness=2):
292293
"""
293294
Turns on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode.
@@ -360,7 +361,6 @@ def __enter__(self):
360361

361362
return dummy_ctx()
362363

363-
364364
## Figures ##
365365

366366
def figure(num=None, # autoincrement if None, else integer from 1-N
@@ -604,10 +604,8 @@ def close(*args):
604604
else:
605605
raise TypeError('close takes 0 or 1 arguments')
606606

607-
608607
## Axes ##
609608

610-
611609
def axes(arg=None, **kwargs):
612610
"""
613611
Add an axes to the current figure and make it the current axes.
@@ -683,8 +681,10 @@ def axes(arg=None, **kwargs):
683681

684682
def delaxes(ax=None):
685683
"""
686-
Remove the given `Axes` *ax* from the current figure. If *ax* is *None*,
687-
the current axes is removed. A KeyError is raised if the axes doesn't exist.
684+
Remove the given `Axes` *ax* from the current figure.
685+
686+
If *ax* is *None*, the current axes is removed. A KeyError is raised if the
687+
axes doesn't exist.
688688
"""
689689
if ax is None:
690690
ax = gca()
@@ -736,12 +736,13 @@ def subplot(*args, **kwargs):
736736
import matplotlib.pyplot as plt
737737
# plot a line, implicitly creating a subplot(111)
738738
plt.plot([1,2,3])
739-
# now create a subplot which represents the top plot of a grid
740-
# with 2 rows and 1 column. Since this subplot will overlap the
741-
# first, the plot (and its axes) previously created, will be removed
739+
# now create a subplot which represents the top plot of a grid with
740+
# 2 rows and 1 column. Since this subplot will overlap the first, the
741+
# plot (and its axes) previously created, will be removed
742742
plt.subplot(211)
743743
plt.plot(range(12))
744-
plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background
744+
# create a second subplot with yellow background
745+
plt.subplot(212, facecolor='y')
745746
746747
If you do not want this behavior, use the
747748
:meth:`~matplotlib.figure.Figure.add_subplot` method or the
@@ -778,28 +779,26 @@ def subplot(*args, **kwargs):
778779
779780
"""
780781
# if subplot called without arguments, create subplot(1,1,1)
781-
if len(args)==0:
782-
args=(1,1,1)
782+
if len(args) == 0:
783+
args = (1, 1, 1)
783784

784785
# This check was added because it is very easy to type
785786
# subplot(1, 2, False) when subplots(1, 2, False) was intended
786787
# (sharex=False, that is). In most cases, no error will
787788
# ever occur, but mysterious behavior can result because what was
788789
# intended to be the sharex argument is instead treated as a
789790
# subplot index for subplot()
790-
if len(args) >= 3 and isinstance(args[2], bool) :
791-
warnings.warn("The subplot index argument to subplot() appears"
792-
" to be a boolean. Did you intend to use subplots()?")
791+
if len(args) >= 3 and isinstance(args[2], bool):
792+
warnings.warn("The subplot index argument to subplot() appears "
793+
"to be a boolean. Did you intend to use subplots()?")
793794

794795
fig = gcf()
795796
a = fig.add_subplot(*args, **kwargs)
796797
bbox = a.bbox
797-
byebye = []
798-
for other in fig.axes:
799-
if other==a: continue
800-
if bbox.fully_overlaps(other.bbox):
801-
byebye.append(other)
802-
for ax in byebye: delaxes(ax)
798+
byebye = [other for other in fig.axes
799+
if other is not a and bbox.fully_overlaps(other.bbox)]
800+
for ax in byebye:
801+
delaxes(ax)
803802

804803
return a
805804

@@ -1021,24 +1020,28 @@ def subplot_tool(targetfig=None):
10211020
"""
10221021
Launch a subplot tool window for a figure.
10231022
1024-
A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1023+
Returns
1024+
-------
1025+
`matplotlib.widgets.SubplotTool`
10251026
"""
1026-
tbar = rcParams['toolbar'] # turn off the navigation toolbar for the toolfig
1027-
rcParams['toolbar'] = 'None'
1027+
tbar = rcParams["toolbar"] # Turn off the nav toolbar for the toolfig.
1028+
rcParams["toolbar"] = "None"
10281029
if targetfig is None:
10291030
manager = get_current_fig_manager()
10301031
targetfig = manager.canvas.figure
10311032
else:
1032-
# find the manager for this figure
1033+
# Find the manager for this figure.
10331034
for manager in _pylab_helpers.Gcf._activeQue:
1034-
if manager.canvas.figure==targetfig: break
1035-
else: raise RuntimeError('Could not find manager for targetfig')
1035+
if manager.canvas.figure == targetfig:
1036+
break
1037+
else:
1038+
raise RuntimeError("Could not find manager for targetfig")
10361039

1037-
toolfig = figure(figsize=(6,3))
1040+
toolfig = figure(figsize=(6, 3))
10381041
toolfig.subplots_adjust(top=0.9)
1039-
ret = SubplotTool(targetfig, toolfig)
1040-
rcParams['toolbar'] = tbar
1041-
_pylab_helpers.Gcf.set_active(manager) # restore the current figure
1042+
ret = SubplotTool(targetfig, toolfig)
1043+
rcParams["toolbar"] = tbar
1044+
_pylab_helpers.Gcf.set_active(manager) # Restore the current figure.
10421045
return ret
10431046

10441047

@@ -1057,10 +1060,8 @@ def box(on=None):
10571060
on = not ax.get_frame_on()
10581061
ax.set_frame_on(on)
10591062

1060-
10611063
## Axis ##
10621064

1063-
10641065
def xlim(*args, **kwargs):
10651066
"""
10661067
Get or set the *x* limits of the current axes.
@@ -1225,15 +1226,14 @@ def rgrids(*args, **kwargs):
12251226
"""
12261227
ax = gca()
12271228
if not isinstance(ax, PolarAxes):
1228-
raise RuntimeError('rgrids only defined for polar axes')
1229-
if len(args)==0:
1229+
raise RuntimeError("rgrids only defined for polar axes")
1230+
if len(args) == 0:
12301231
lines = ax.yaxis.get_gridlines()
12311232
labels = ax.yaxis.get_ticklabels()
12321233
else:
12331234
lines, labels = ax.set_rgrids(*args, **kwargs)
1234-
1235-
return ( silent_list('Line2D rgridline', lines),
1236-
silent_list('Text rgridlabel', labels) )
1235+
return (silent_list("Line2D rgridline", lines),
1236+
silent_list("Text rgridlabel", labels))
12371237

12381238

12391239
def thetagrids(*args, **kwargs):
@@ -1271,31 +1271,27 @@ def thetagrids(*args, **kwargs):
12711271
12721272
- *labels* are :class:`~matplotlib.text.Text` instances.
12731273
1274-
Note that on input, the *labels* argument is a list of strings,
1275-
and on output it is a list of :class:`~matplotlib.text.Text`
1276-
instances.
1274+
Note that on input, the *labels* argument is a list of strings, and on
1275+
output it is a list of :class:`~matplotlib.text.Text` instances.
12771276
12781277
Examples::
12791278
12801279
# set the locations of the radial gridlines and labels
1281-
lines, labels = thetagrids( range(45,360,90) )
1280+
lines, labels = thetagrids(range(45, 360, 90))
12821281
12831282
# set the locations and labels of the radial gridlines and labels
1284-
lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1283+
lines, labels = thetagrids(range(45, 360, 90), ('NE', 'NW', 'SW', 'SE'))
12851284
"""
12861285
ax = gca()
12871286
if not isinstance(ax, PolarAxes):
1288-
raise RuntimeError('rgrids only defined for polar axes')
1289-
if len(args)==0:
1287+
raise RuntimeError("rgrids only defined for polar axes")
1288+
if len(args) == 0:
12901289
lines = ax.xaxis.get_ticklines()
12911290
labels = ax.xaxis.get_ticklabels()
12921291
else:
12931292
lines, labels = ax.set_thetagrids(*args, **kwargs)
1294-
1295-
return (silent_list('Line2D thetagridline', lines),
1296-
silent_list('Text thetagridlabel', labels)
1297-
)
1298-
1293+
return (silent_list("Line2D thetagridline", lines),
1294+
silent_list("Text thetagridlabel", labels))
12991295

13001296
## Plotting Info ##
13011297

@@ -1362,16 +1358,15 @@ def colors():
13621358
Here is an example that creates a pale turquoise title::
13631359
13641360
title('Is this the best color?', color='#afeeee')
1365-
13661361
"""
1367-
pass
13681362

13691363

13701364
def colormaps():
13711365
"""
13721366
Matplotlib provides a number of colormaps, and others can be added using
1373-
:func:`~matplotlib.cm.register_cmap`. This function documents the built-in
1374-
colormaps, and will also return a list of all registered colormaps if called.
1367+
`~matplotlib.cm.register_cmap`. This function documents the built-in
1368+
colormaps, and will also return a list of all registered colormaps if
1369+
called.
13751370
13761371
You can set the colormap for an image, pcolor, scatter, etc,
13771372
using a keyword argument::
@@ -1628,7 +1623,7 @@ def pad(s, l):
16281623
exclude = {"colormaps", "colors", "connect", "disconnect",
16291624
"get_current_fig_manager", "ginput", "plotting",
16301625
"waitforbuttonpress"}
1631-
commands = sorted(set(__all__) - exclude - set(colormaps()))
1626+
commands = sorted(set(__all__) - exclude - set(colormaps()))
16321627

16331628
first_sentence = re.compile(r"(?:\s*).+?\.(?:\s+|$)", flags=re.DOTALL)
16341629

@@ -1676,9 +1671,7 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
16761671
'with contourf).')
16771672
if ax is None:
16781673
ax = gca()
1679-
1680-
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
1681-
return ret
1674+
return gcf().colorbar(mappable, cax=cax, ax=ax, **kw)
16821675
colorbar.__doc__ = matplotlib.colorbar.colorbar_doc
16831676

16841677

@@ -1743,7 +1736,6 @@ def matshow(A, fignum=None, **kw):
17431736
kwarg to "lower" if you want the first row in the array to be
17441737
at the bottom instead of the top.
17451738
1746-
17471739
*fignum*: [ None | integer | False ]
17481740
By default, :func:`matshow` creates a new figure window with
17491741
automatic numbering. If *fignum* is given as an integer, the
@@ -1758,9 +1750,9 @@ def matshow(A, fignum=None, **kw):
17581750
if fignum is False or fignum is 0:
17591751
ax = gca()
17601752
else:
1761-
# Extract actual aspect ratio of array and make appropriately sized figure
1753+
# Extract array's actual aspect ratio; make appropriately sized figure.
17621754
fig = figure(fignum, figsize=figaspect(A))
1763-
ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
1755+
ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
17641756

17651757
im = ax.matshow(A, **kw)
17661758
sci(im)

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pep8ignore =
6666
matplotlib/mathtext.py E201 E202 E203 E211 E221 E222 E225 E228 E231 E251 E261 E301 E302 E303 E401 E402 E501
6767
matplotlib/patheffects.py E231
6868
matplotlib/pylab.py E401 E402 E501
69-
matplotlib/pyplot.py E201 E202 E203 E221 E222 E225 E231 E251 E261 E302 E303 E501 E701 E713
69+
matplotlib/pyplot.py E302 E305
7070
matplotlib/rcsetup.py E203 E225 E261 E302 E501
7171
matplotlib/stackplot.py E251
7272
matplotlib/transforms.py E201 E202 E203 E302 E501

0 commit comments

Comments
 (0)