63
63
MaxNLocator
64
64
from matplotlib .backends import pylab_setup
65
65
66
+
66
67
## Backend detection ##
68
+
67
69
def _backend_selection ():
68
70
""" If rcParams['backend_fallback'] is true, check to see if the
69
71
current backend is compatible with the current running event
@@ -73,7 +75,7 @@ def _backend_selection():
73
75
if not rcParams ['backend_fallback' ] or backend not in _interactive_bk :
74
76
return
75
77
is_agg_backend = rcParams ['backend' ].endswith ('Agg' )
76
- if 'wx' in sys .modules and not backend in ('WX' , 'WXAgg' ):
78
+ if 'wx' in sys .modules and backend not in ('WX' , 'WXAgg' ):
77
79
import wx
78
80
if wx .App .IsMainLoopRunning ():
79
81
rcParams ['backend' ] = 'wx' + 'Agg' * is_agg_backend
@@ -223,7 +225,8 @@ def switch_backend(newbackend):
223
225
global _backend_mod , new_figure_manager , draw_if_interactive , _show
224
226
matplotlib .use (newbackend , warn = False , force = True )
225
227
from matplotlib .backends import pylab_setup
226
- _backend_mod , new_figure_manager , draw_if_interactive , _show = pylab_setup ()
228
+ _backend_mod , new_figure_manager , draw_if_interactive , _show = \
229
+ pylab_setup ()
227
230
228
231
229
232
def show (* args , ** kw ):
@@ -281,10 +284,8 @@ def pause(interval):
281
284
else :
282
285
time .sleep (interval )
283
286
284
-
285
287
## Any Artist ##
286
288
287
-
288
289
def xkcd (scale = 1 , length = 100 , randomness = 2 ):
289
290
"""
290
291
Turns on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode.
@@ -357,7 +358,6 @@ def __enter__(self):
357
358
358
359
return dummy_ctx ()
359
360
360
-
361
361
## Figures ##
362
362
363
363
def figure (num = None , # autoincrement if None, else integer from 1-N
@@ -602,10 +602,8 @@ def close(*args):
602
602
else :
603
603
raise TypeError ('close takes 0 or 1 arguments' )
604
604
605
-
606
605
## Axes ##
607
606
608
-
609
607
def axes (* args , ** kwargs ):
610
608
"""
611
609
Add an axes to the figure.
@@ -728,12 +726,13 @@ def subplot(*args, **kwargs):
728
726
import matplotlib.pyplot as plt
729
727
# plot a line, implicitly creating a subplot(111)
730
728
plt.plot([1,2,3])
731
- # now create a subplot which represents the top plot of a grid
732
- # with 2 rows and 1 column. Since this subplot will overlap the
733
- # first, the plot (and its axes) previously created, will be removed
729
+ # now create a subplot which represents the top plot of a grid with
730
+ # 2 rows and 1 column. Since this subplot will overlap the first, the
731
+ # plot (and its axes) previously created, will be removed
734
732
plt.subplot(211)
735
733
plt.plot(range(12))
736
- plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background
734
+ # create a second subplot with yellow background
735
+ plt.subplot(212, facecolor='y')
737
736
738
737
If you do not want this behavior, use the
739
738
:meth:`~matplotlib.figure.Figure.add_subplot` method or the
@@ -770,28 +769,26 @@ def subplot(*args, **kwargs):
770
769
771
770
"""
772
771
# if subplot called without arguments, create subplot(1,1,1)
773
- if len (args )== 0 :
774
- args = (1 ,1 , 1 )
772
+ if len (args ) == 0 :
773
+ args = (1 , 1 , 1 )
775
774
776
775
# This check was added because it is very easy to type
777
776
# subplot(1, 2, False) when subplots(1, 2, False) was intended
778
777
# (sharex=False, that is). In most cases, no error will
779
778
# ever occur, but mysterious behavior can result because what was
780
779
# intended to be the sharex argument is instead treated as a
781
780
# subplot index for subplot()
782
- if len (args ) >= 3 and isinstance (args [2 ], bool ) :
783
- warnings .warn ("The subplot index argument to subplot() appears"
784
- " to be a boolean. Did you intend to use subplots()?" )
781
+ if len (args ) >= 3 and isinstance (args [2 ], bool ):
782
+ warnings .warn ("The subplot index argument to subplot() appears "
783
+ "to be a boolean. Did you intend to use subplots()?" )
785
784
786
785
fig = gcf ()
787
786
a = fig .add_subplot (* args , ** kwargs )
788
787
bbox = a .bbox
789
- byebye = []
790
- for other in fig .axes :
791
- if other == a : continue
792
- if bbox .fully_overlaps (other .bbox ):
793
- byebye .append (other )
794
- for ax in byebye : delaxes (ax )
788
+ byebye = [other for other in fig .axes
789
+ if other is not a and bbox .fully_overlaps (other .bbox )]
790
+ for ax in byebye :
791
+ delaxes (ax )
795
792
796
793
return a
797
794
@@ -1013,24 +1010,28 @@ def subplot_tool(targetfig=None):
1013
1010
"""
1014
1011
Launch a subplot tool window for a figure.
1015
1012
1016
- A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1013
+ Returns
1014
+ -------
1015
+ `matplotlib.widgets.SubplotTool`
1017
1016
"""
1018
- tbar = rcParams [' toolbar' ] # turn off the navigation toolbar for the toolfig
1019
- rcParams [' toolbar' ] = ' None'
1017
+ tbar = rcParams [" toolbar" ] # Turn off the nav toolbar for the toolfig.
1018
+ rcParams [" toolbar" ] = " None"
1020
1019
if targetfig is None :
1021
1020
manager = get_current_fig_manager ()
1022
1021
targetfig = manager .canvas .figure
1023
1022
else :
1024
- # find the manager for this figure
1023
+ # Find the manager for this figure.
1025
1024
for manager in _pylab_helpers .Gcf ._activeQue :
1026
- if manager .canvas .figure == targetfig : break
1027
- else : raise RuntimeError ('Could not find manager for targetfig' )
1025
+ if manager .canvas .figure == targetfig :
1026
+ break
1027
+ else :
1028
+ raise RuntimeError ("Could not find manager for targetfig" )
1028
1029
1029
- toolfig = figure (figsize = (6 ,3 ))
1030
+ toolfig = figure (figsize = (6 , 3 ))
1030
1031
toolfig .subplots_adjust (top = 0.9 )
1031
- ret = SubplotTool (targetfig , toolfig )
1032
- rcParams [' toolbar' ] = tbar
1033
- _pylab_helpers .Gcf .set_active (manager ) # restore the current figure
1032
+ ret = SubplotTool (targetfig , toolfig )
1033
+ rcParams [" toolbar" ] = tbar
1034
+ _pylab_helpers .Gcf .set_active (manager ) # Restore the current figure.
1034
1035
return ret
1035
1036
1036
1037
@@ -1047,10 +1048,8 @@ def box(on=None):
1047
1048
on = not ax .get_frame_on ()
1048
1049
ax .set_frame_on (on )
1049
1050
1050
-
1051
1051
## Axis ##
1052
1052
1053
-
1054
1053
def xlim (* args , ** kwargs ):
1055
1054
"""
1056
1055
Get or set the *x* limits of the current axes.
@@ -1215,15 +1214,14 @@ def rgrids(*args, **kwargs):
1215
1214
"""
1216
1215
ax = gca ()
1217
1216
if not isinstance (ax , PolarAxes ):
1218
- raise RuntimeError (' rgrids only defined for polar axes' )
1219
- if len (args )== 0 :
1217
+ raise RuntimeError (" rgrids only defined for polar axes" )
1218
+ if len (args ) == 0 :
1220
1219
lines = ax .yaxis .get_gridlines ()
1221
1220
labels = ax .yaxis .get_ticklabels ()
1222
1221
else :
1223
1222
lines , labels = ax .set_rgrids (* args , ** kwargs )
1224
-
1225
- return ( silent_list ('Line2D rgridline' , lines ),
1226
- silent_list ('Text rgridlabel' , labels ) )
1223
+ return (silent_list ("Line2D rgridline" , lines ),
1224
+ silent_list ("Text rgridlabel" , labels ))
1227
1225
1228
1226
1229
1227
def thetagrids (* args , ** kwargs ):
@@ -1261,31 +1259,27 @@ def thetagrids(*args, **kwargs):
1261
1259
1262
1260
- *labels* are :class:`~matplotlib.text.Text` instances.
1263
1261
1264
- Note that on input, the *labels* argument is a list of strings,
1265
- and on output it is a list of :class:`~matplotlib.text.Text`
1266
- instances.
1262
+ Note that on input, the *labels* argument is a list of strings, and on
1263
+ output it is a list of :class:`~matplotlib.text.Text` instances.
1267
1264
1268
1265
Examples::
1269
1266
1270
1267
# set the locations of the radial gridlines and labels
1271
- lines, labels = thetagrids( range(45,360,90) )
1268
+ lines, labels = thetagrids(range(45, 360, 90))
1272
1269
1273
1270
# set the locations and labels of the radial gridlines and labels
1274
- lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1271
+ lines, labels = thetagrids(range(45, 360, 90), ('NE', 'NW', 'SW', 'SE'))
1275
1272
"""
1276
1273
ax = gca ()
1277
1274
if not isinstance (ax , PolarAxes ):
1278
- raise RuntimeError (' rgrids only defined for polar axes' )
1279
- if len (args )== 0 :
1275
+ raise RuntimeError (" rgrids only defined for polar axes" )
1276
+ if len (args ) == 0 :
1280
1277
lines = ax .xaxis .get_ticklines ()
1281
1278
labels = ax .xaxis .get_ticklabels ()
1282
1279
else :
1283
1280
lines , labels = ax .set_thetagrids (* args , ** kwargs )
1284
-
1285
- return (silent_list ('Line2D thetagridline' , lines ),
1286
- silent_list ('Text thetagridlabel' , labels )
1287
- )
1288
-
1281
+ return (silent_list ("Line2D thetagridline" , lines ),
1282
+ silent_list ("Text thetagridlabel" , labels ))
1289
1283
1290
1284
## Plotting Info ##
1291
1285
@@ -1352,16 +1346,15 @@ def colors():
1352
1346
Here is an example that creates a pale turquoise title::
1353
1347
1354
1348
title('Is this the best color?', color='#afeeee')
1355
-
1356
1349
"""
1357
- pass
1358
1350
1359
1351
1360
1352
def colormaps ():
1361
1353
"""
1362
1354
Matplotlib provides a number of colormaps, and others can be added using
1363
- :func:`~matplotlib.cm.register_cmap`. This function documents the built-in
1364
- colormaps, and will also return a list of all registered colormaps if called.
1355
+ `~matplotlib.cm.register_cmap`. This function documents the built-in
1356
+ colormaps, and will also return a list of all registered colormaps if
1357
+ called.
1365
1358
1366
1359
You can set the colormap for an image, pcolor, scatter, etc,
1367
1360
using a keyword argument::
@@ -1618,7 +1611,7 @@ def pad(s, l):
1618
1611
exclude = {"colormaps" , "colors" , "connect" , "disconnect" ,
1619
1612
"get_current_fig_manager" , "ginput" , "plotting" ,
1620
1613
"waitforbuttonpress" }
1621
- commands = sorted (set (__all__ ) - exclude - set (colormaps ()))
1614
+ commands = sorted (set (__all__ ) - exclude - set (colormaps ()))
1622
1615
1623
1616
first_sentence = re .compile (r"(?:\s*).+?\.(?:\s+|$)" , flags = re .DOTALL )
1624
1617
@@ -1666,9 +1659,7 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
1666
1659
'with contourf).' )
1667
1660
if ax is None :
1668
1661
ax = gca ()
1669
-
1670
- ret = gcf ().colorbar (mappable , cax = cax , ax = ax , ** kw )
1671
- return ret
1662
+ return gcf ().colorbar (mappable , cax = cax , ax = ax , ** kw )
1672
1663
colorbar .__doc__ = matplotlib .colorbar .colorbar_doc
1673
1664
1674
1665
@@ -1733,7 +1724,6 @@ def matshow(A, fignum=None, **kw):
1733
1724
kwarg to "lower" if you want the first row in the array to be
1734
1725
at the bottom instead of the top.
1735
1726
1736
-
1737
1727
*fignum*: [ None | integer | False ]
1738
1728
By default, :func:`matshow` creates a new figure window with
1739
1729
automatic numbering. If *fignum* is given as an integer, the
@@ -1748,9 +1738,9 @@ def matshow(A, fignum=None, **kw):
1748
1738
if fignum is False or fignum is 0 :
1749
1739
ax = gca ()
1750
1740
else :
1751
- # Extract actual aspect ratio of array and make appropriately sized figure
1741
+ # Extract array's actual aspect ratio; make appropriately sized figure.
1752
1742
fig = figure (fignum , figsize = figaspect (A ))
1753
- ax = fig .add_axes ([0.15 , 0.09 , 0.775 , 0.775 ])
1743
+ ax = fig .add_axes ([0.15 , 0.09 , 0.775 , 0.775 ])
1754
1744
1755
1745
im = ax .matshow (A , ** kw )
1756
1746
sci (im )
0 commit comments