64
64
MaxNLocator
65
65
from matplotlib .backends import pylab_setup
66
66
67
+
67
68
## Backend detection ##
69
+
68
70
def _backend_selection ():
69
71
""" If rcParams['backend_fallback'] is true, check to see if the
70
72
current backend is compatible with the current running event
@@ -74,7 +76,7 @@ def _backend_selection():
74
76
if not rcParams ['backend_fallback' ] or backend not in _interactive_bk :
75
77
return
76
78
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' ):
78
80
import wx
79
81
if wx .App .IsMainLoopRunning ():
80
82
rcParams ['backend' ] = 'wx' + 'Agg' * is_agg_backend
@@ -224,7 +226,8 @@ def switch_backend(newbackend):
224
226
global _backend_mod , new_figure_manager , draw_if_interactive , _show
225
227
matplotlib .use (newbackend , warn = False , force = True )
226
228
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 ()
228
231
229
232
230
233
def show (* args , ** kw ):
@@ -284,10 +287,8 @@ def pause(interval):
284
287
else :
285
288
time .sleep (interval )
286
289
287
-
288
290
## Any Artist ##
289
291
290
-
291
292
def xkcd (scale = 1 , length = 100 , randomness = 2 ):
292
293
"""
293
294
Turns on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode.
@@ -360,7 +361,6 @@ def __enter__(self):
360
361
361
362
return dummy_ctx ()
362
363
363
-
364
364
## Figures ##
365
365
366
366
def figure (num = None , # autoincrement if None, else integer from 1-N
@@ -604,10 +604,8 @@ def close(*args):
604
604
else :
605
605
raise TypeError ('close takes 0 or 1 arguments' )
606
606
607
-
608
607
## Axes ##
609
608
610
-
611
609
def axes (arg = None , ** kwargs ):
612
610
"""
613
611
Add an axes to the current figure and make it the current axes.
@@ -739,12 +737,13 @@ def subplot(*args, **kwargs):
739
737
import matplotlib.pyplot as plt
740
738
# plot a line, implicitly creating a subplot(111)
741
739
plt.plot([1,2,3])
742
- # now create a subplot which represents the top plot of a grid
743
- # with 2 rows and 1 column. Since this subplot will overlap the
744
- # first, the plot (and its axes) previously created, will be removed
740
+ # now create a subplot which represents the top plot of a grid with
741
+ # 2 rows and 1 column. Since this subplot will overlap the first, the
742
+ # plot (and its axes) previously created, will be removed
745
743
plt.subplot(211)
746
744
plt.plot(range(12))
747
- plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background
745
+ # create a second subplot with yellow background
746
+ plt.subplot(212, facecolor='y')
748
747
749
748
If you do not want this behavior, use the
750
749
:meth:`~matplotlib.figure.Figure.add_subplot` method or the
@@ -781,28 +780,26 @@ def subplot(*args, **kwargs):
781
780
782
781
"""
783
782
# if subplot called without arguments, create subplot(1,1,1)
784
- if len (args )== 0 :
785
- args = (1 ,1 , 1 )
783
+ if len (args ) == 0 :
784
+ args = (1 , 1 , 1 )
786
785
787
786
# This check was added because it is very easy to type
788
787
# subplot(1, 2, False) when subplots(1, 2, False) was intended
789
788
# (sharex=False, that is). In most cases, no error will
790
789
# ever occur, but mysterious behavior can result because what was
791
790
# intended to be the sharex argument is instead treated as a
792
791
# subplot index for subplot()
793
- if len (args ) >= 3 and isinstance (args [2 ], bool ) :
794
- warnings .warn ("The subplot index argument to subplot() appears"
795
- " to be a boolean. Did you intend to use subplots()?" )
792
+ if len (args ) >= 3 and isinstance (args [2 ], bool ):
793
+ warnings .warn ("The subplot index argument to subplot() appears "
794
+ "to be a boolean. Did you intend to use subplots()?" )
796
795
797
796
fig = gcf ()
798
797
a = fig .add_subplot (* args , ** kwargs )
799
798
bbox = a .bbox
800
- byebye = []
801
- for other in fig .axes :
802
- if other == a : continue
803
- if bbox .fully_overlaps (other .bbox ):
804
- byebye .append (other )
805
- for ax in byebye : delaxes (ax )
799
+ byebye = [other for other in fig .axes
800
+ if other is not a and bbox .fully_overlaps (other .bbox )]
801
+ for ax in byebye :
802
+ delaxes (ax )
806
803
807
804
return a
808
805
@@ -1024,24 +1021,28 @@ def subplot_tool(targetfig=None):
1024
1021
"""
1025
1022
Launch a subplot tool window for a figure.
1026
1023
1027
- A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1024
+ Returns
1025
+ -------
1026
+ `matplotlib.widgets.SubplotTool`
1028
1027
"""
1029
- tbar = rcParams [' toolbar' ] # turn off the navigation toolbar for the toolfig
1030
- rcParams [' toolbar' ] = ' None'
1028
+ tbar = rcParams [" toolbar" ] # Turn off the nav toolbar for the toolfig.
1029
+ rcParams [" toolbar" ] = " None"
1031
1030
if targetfig is None :
1032
1031
manager = get_current_fig_manager ()
1033
1032
targetfig = manager .canvas .figure
1034
1033
else :
1035
- # find the manager for this figure
1034
+ # Find the manager for this figure.
1036
1035
for manager in _pylab_helpers .Gcf ._activeQue :
1037
- if manager .canvas .figure == targetfig : break
1038
- else : raise RuntimeError ('Could not find manager for targetfig' )
1036
+ if manager .canvas .figure == targetfig :
1037
+ break
1038
+ else :
1039
+ raise RuntimeError ("Could not find manager for targetfig" )
1039
1040
1040
- toolfig = figure (figsize = (6 ,3 ))
1041
+ toolfig = figure (figsize = (6 , 3 ))
1041
1042
toolfig .subplots_adjust (top = 0.9 )
1042
- ret = SubplotTool (targetfig , toolfig )
1043
- rcParams [' toolbar' ] = tbar
1044
- _pylab_helpers .Gcf .set_active (manager ) # restore the current figure
1043
+ ret = SubplotTool (targetfig , toolfig )
1044
+ rcParams [" toolbar" ] = tbar
1045
+ _pylab_helpers .Gcf .set_active (manager ) # Restore the current figure.
1045
1046
return ret
1046
1047
1047
1048
@@ -1058,10 +1059,8 @@ def box(on=None):
1058
1059
on = not ax .get_frame_on ()
1059
1060
ax .set_frame_on (on )
1060
1061
1061
-
1062
1062
## Axis ##
1063
1063
1064
-
1065
1064
def xlim (* args , ** kwargs ):
1066
1065
"""
1067
1066
Get or set the *x* limits of the current axes.
@@ -1226,15 +1225,14 @@ def rgrids(*args, **kwargs):
1226
1225
"""
1227
1226
ax = gca ()
1228
1227
if not isinstance (ax , PolarAxes ):
1229
- raise RuntimeError (' rgrids only defined for polar axes' )
1230
- if len (args )== 0 :
1228
+ raise RuntimeError (" rgrids only defined for polar axes" )
1229
+ if len (args ) == 0 :
1231
1230
lines = ax .yaxis .get_gridlines ()
1232
1231
labels = ax .yaxis .get_ticklabels ()
1233
1232
else :
1234
1233
lines , labels = ax .set_rgrids (* args , ** kwargs )
1235
-
1236
- return ( silent_list ('Line2D rgridline' , lines ),
1237
- silent_list ('Text rgridlabel' , labels ) )
1234
+ return (silent_list ("Line2D rgridline" , lines ),
1235
+ silent_list ("Text rgridlabel" , labels ))
1238
1236
1239
1237
1240
1238
def thetagrids (* args , ** kwargs ):
@@ -1272,31 +1270,27 @@ def thetagrids(*args, **kwargs):
1272
1270
1273
1271
- *labels* are :class:`~matplotlib.text.Text` instances.
1274
1272
1275
- Note that on input, the *labels* argument is a list of strings,
1276
- and on output it is a list of :class:`~matplotlib.text.Text`
1277
- instances.
1273
+ Note that on input, the *labels* argument is a list of strings, and on
1274
+ output it is a list of :class:`~matplotlib.text.Text` instances.
1278
1275
1279
1276
Examples::
1280
1277
1281
1278
# set the locations of the radial gridlines and labels
1282
- lines, labels = thetagrids( range(45,360,90) )
1279
+ lines, labels = thetagrids(range(45, 360, 90))
1283
1280
1284
1281
# set the locations and labels of the radial gridlines and labels
1285
- lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1282
+ lines, labels = thetagrids(range(45, 360, 90), ('NE', 'NW', 'SW', 'SE'))
1286
1283
"""
1287
1284
ax = gca ()
1288
1285
if not isinstance (ax , PolarAxes ):
1289
- raise RuntimeError (' rgrids only defined for polar axes' )
1290
- if len (args )== 0 :
1286
+ raise RuntimeError (" rgrids only defined for polar axes" )
1287
+ if len (args ) == 0 :
1291
1288
lines = ax .xaxis .get_ticklines ()
1292
1289
labels = ax .xaxis .get_ticklabels ()
1293
1290
else :
1294
1291
lines , labels = ax .set_thetagrids (* args , ** kwargs )
1295
-
1296
- return (silent_list ('Line2D thetagridline' , lines ),
1297
- silent_list ('Text thetagridlabel' , labels )
1298
- )
1299
-
1292
+ return (silent_list ("Line2D thetagridline" , lines ),
1293
+ silent_list ("Text thetagridlabel" , labels ))
1300
1294
1301
1295
## Plotting Info ##
1302
1296
@@ -1363,16 +1357,15 @@ def colors():
1363
1357
Here is an example that creates a pale turquoise title::
1364
1358
1365
1359
title('Is this the best color?', color='#afeeee')
1366
-
1367
1360
"""
1368
- pass
1369
1361
1370
1362
1371
1363
def colormaps ():
1372
1364
"""
1373
1365
Matplotlib provides a number of colormaps, and others can be added using
1374
- :func:`~matplotlib.cm.register_cmap`. This function documents the built-in
1375
- colormaps, and will also return a list of all registered colormaps if called.
1366
+ `~matplotlib.cm.register_cmap`. This function documents the built-in
1367
+ colormaps, and will also return a list of all registered colormaps if
1368
+ called.
1376
1369
1377
1370
You can set the colormap for an image, pcolor, scatter, etc,
1378
1371
using a keyword argument::
@@ -1629,7 +1622,7 @@ def pad(s, l):
1629
1622
exclude = {"colormaps" , "colors" , "connect" , "disconnect" ,
1630
1623
"get_current_fig_manager" , "ginput" , "plotting" ,
1631
1624
"waitforbuttonpress" }
1632
- commands = sorted (set (__all__ ) - exclude - set (colormaps ()))
1625
+ commands = sorted (set (__all__ ) - exclude - set (colormaps ()))
1633
1626
1634
1627
first_sentence = re .compile (r"(?:\s*).+?\.(?:\s+|$)" , flags = re .DOTALL )
1635
1628
@@ -1677,9 +1670,7 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
1677
1670
'with contourf).' )
1678
1671
if ax is None :
1679
1672
ax = gca ()
1680
-
1681
- ret = gcf ().colorbar (mappable , cax = cax , ax = ax , ** kw )
1682
- return ret
1673
+ return gcf ().colorbar (mappable , cax = cax , ax = ax , ** kw )
1683
1674
colorbar .__doc__ = matplotlib .colorbar .colorbar_doc
1684
1675
1685
1676
@@ -1744,7 +1735,6 @@ def matshow(A, fignum=None, **kw):
1744
1735
kwarg to "lower" if you want the first row in the array to be
1745
1736
at the bottom instead of the top.
1746
1737
1747
-
1748
1738
*fignum*: [ None | integer | False ]
1749
1739
By default, :func:`matshow` creates a new figure window with
1750
1740
automatic numbering. If *fignum* is given as an integer, the
@@ -1759,9 +1749,9 @@ def matshow(A, fignum=None, **kw):
1759
1749
if fignum is False or fignum is 0 :
1760
1750
ax = gca ()
1761
1751
else :
1762
- # Extract actual aspect ratio of array and make appropriately sized figure
1752
+ # Extract array's actual aspect ratio; make appropriately sized figure.
1763
1753
fig = figure (fignum , figsize = figaspect (A ))
1764
- ax = fig .add_axes ([0.15 , 0.09 , 0.775 , 0.775 ])
1754
+ ax = fig .add_axes ([0.15 , 0.09 , 0.775 , 0.775 ])
1765
1755
1766
1756
im = ax .matshow (A , ** kw )
1767
1757
sci (im )
0 commit comments