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.
@@ -722,12 +720,13 @@ def subplot(*args, **kwargs):
722
720
import matplotlib.pyplot as plt
723
721
# plot a line, implicitly creating a subplot(111)
724
722
plt.plot([1,2,3])
725
- # now create a subplot which represents the top plot of a grid
726
- #with 2 rows and 1 column. Since this subplot will overlap the
727
- #first, the plot (and its axes) previously created, will be removed
723
+ # now create a subplot which represents the top plot of a grid with
724
+ # 2 rows and 1 column. Since this subplot will overlap the first, the
725
+ # plot (and its axes) previously created, will be removed
728
726
plt.subplot(211)
729
727
plt.plot(range(12))
730
- plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background
728
+ # create a second subplot with yellow background
729
+ plt.subplot(212, facecolor='y')
731
730
732
731
If you do not want this behavior, use the
733
732
:meth:`~matplotlib.figure.Figure.add_subplot` method or the
@@ -764,28 +763,26 @@ def subplot(*args, **kwargs):
764
763
765
764
"""
766
765
# if subplot called without arguments, create subplot(1,1,1)
767
- if len (args )== 0 :
768
- args = (1 ,1 , 1 )
766
+ if len (args )== 0 :
767
+ args = (1 ,1 , 1 )
769
768
770
769
# This check was added because it is very easy to type
771
770
# subplot(1, 2, False) when subplots(1, 2, False) was intended
772
771
# (sharex=False, that is). In most cases, no error will
773
772
# ever occur, but mysterious behavior can result because what was
774
773
# intended to be the sharex argument is instead treated as a
775
774
# subplot index for subplot()
776
- if len (args )>= 3 and isinstance (args [2 ],bool ) :
777
- warnings .warn ("The subplot index argument to subplot() appears"
778
- " to be a boolean. Did you intend to use subplots()?" )
775
+ if len (args )>= 3 and isinstance (args [2 ],bool ):
776
+ warnings .warn ("The subplot index argument to subplot() appears "
777
+ "to be a boolean. Did you intend to use subplots()?" )
779
778
780
779
fig = gcf ()
781
780
a = fig .add_subplot (* args ,** kwargs )
782
781
bbox = a .bbox
783
- byebye = []
784
- for other in fig .axes :
785
- if other == a :continue
786
- if bbox .fully_overlaps (other .bbox ):
787
- byebye .append (other )
788
- for ax in byebye :delaxes (ax )
782
+ byebye = [other for other in fig .axes
783
+ if other is not a and bbox .fully_overlaps (other .bbox )]
784
+ for ax in byebye :
785
+ delaxes (ax )
789
786
790
787
return a
791
788
@@ -1007,24 +1004,28 @@ def subplot_tool(targetfig=None):
1007
1004
"""
1008
1005
Launch a subplot tool window for a figure.
1009
1006
1010
- A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1007
+ Returns
1008
+ -------
1009
+ `matplotlib.widgets.SubplotTool`
1011
1010
"""
1012
- tbar = rcParams [' toolbar' ] # turn off thenavigation toolbar for the toolfig
1013
- rcParams [' toolbar' ]= ' None'
1011
+ tbar = rcParams [" toolbar" ] # Turn off thenav toolbar for the toolfig.
1012
+ rcParams [" toolbar" ]= " None"
1014
1013
if targetfig is None :
1015
1014
manager = get_current_fig_manager ()
1016
1015
targetfig = manager .canvas .figure
1017
1016
else :
1018
- #find the manager for this figure
1017
+ #Find the manager for this figure.
1019
1018
for manager in _pylab_helpers .Gcf ._activeQue :
1020
- if manager .canvas .figure == targetfig :break
1021
- else :raise RuntimeError ('Could not find manager for targetfig' )
1019
+ if manager .canvas .figure == targetfig :
1020
+ break
1021
+ else :
1022
+ raise RuntimeError ("Could not find manager for targetfig" )
1022
1023
1023
- toolfig = figure (figsize = (6 ,3 ))
1024
+ toolfig = figure (figsize = (6 ,3 ))
1024
1025
toolfig .subplots_adjust (top = 0.9 )
1025
- ret = SubplotTool (targetfig ,toolfig )
1026
- rcParams [' toolbar' ]= tbar
1027
- _pylab_helpers .Gcf .set_active (manager )#restore the current figure
1026
+ ret = SubplotTool (targetfig ,toolfig )
1027
+ rcParams [" toolbar" ]= tbar
1028
+ _pylab_helpers .Gcf .set_active (manager )#Restore the current figure.
1028
1029
return ret
1029
1030
1030
1031
@@ -1041,10 +1042,8 @@ def box(on=None):
1041
1042
on = not ax .get_frame_on ()
1042
1043
ax .set_frame_on (on )
1043
1044
1044
-
1045
1045
## Axis ##
1046
1046
1047
-
1048
1047
def xlim (* args ,** kwargs ):
1049
1048
"""
1050
1049
Get or set the *x* limits of the current axes.
@@ -1209,15 +1208,14 @@ def rgrids(*args, **kwargs):
1209
1208
"""
1210
1209
ax = gca ()
1211
1210
if not isinstance (ax ,PolarAxes ):
1212
- raise RuntimeError (' rgrids only defined for polar axes' )
1213
- if len (args )== 0 :
1211
+ raise RuntimeError (" rgrids only defined for polar axes" )
1212
+ if len (args )== 0 :
1214
1213
lines = ax .yaxis .get_gridlines ()
1215
1214
labels = ax .yaxis .get_ticklabels ()
1216
1215
else :
1217
1216
lines ,labels = ax .set_rgrids (* args ,** kwargs )
1218
-
1219
- return (silent_list ('Line2D rgridline' ,lines ),
1220
- silent_list ('Text rgridlabel' ,labels ) )
1217
+ return (silent_list ("Line2D rgridline" ,lines ),
1218
+ silent_list ("Text rgridlabel" ,labels ))
1221
1219
1222
1220
1223
1221
def thetagrids (* args ,** kwargs ):
@@ -1255,31 +1253,27 @@ def thetagrids(*args, **kwargs):
1255
1253
1256
1254
- *labels* are :class:`~matplotlib.text.Text` instances.
1257
1255
1258
- Note that on input, the *labels* argument is a list of strings,
1259
- and on output it is a list of :class:`~matplotlib.text.Text`
1260
- instances.
1256
+ Note that on input, the *labels* argument is a list of strings, and on
1257
+ output it is a list of :class:`~matplotlib.text.Text` instances.
1261
1258
1262
1259
Examples::
1263
1260
1264
1261
# set the locations of the radial gridlines and labels
1265
- lines, labels = thetagrids( range(45,360,90) )
1262
+ lines, labels = thetagrids(range(45, 360, 90))
1266
1263
1267
1264
# set the locations and labels of the radial gridlines and labels
1268
- lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1265
+ lines, labels = thetagrids(range(45, 360, 90), ('NE', 'NW', 'SW', 'SE'))
1269
1266
"""
1270
1267
ax = gca ()
1271
1268
if not isinstance (ax ,PolarAxes ):
1272
- raise RuntimeError (' rgrids only defined for polar axes' )
1273
- if len (args )== 0 :
1269
+ raise RuntimeError (" rgrids only defined for polar axes" )
1270
+ if len (args )== 0 :
1274
1271
lines = ax .xaxis .get_ticklines ()
1275
1272
labels = ax .xaxis .get_ticklabels ()
1276
1273
else :
1277
1274
lines ,labels = ax .set_thetagrids (* args ,** kwargs )
1278
-
1279
- return (silent_list ('Line2D thetagridline' ,lines ),
1280
- silent_list ('Text thetagridlabel' ,labels )
1281
- )
1282
-
1275
+ return (silent_list ("Line2D thetagridline" ,lines ),
1276
+ silent_list ("Text thetagridlabel" ,labels ))
1283
1277
1284
1278
## Plotting Info ##
1285
1279
@@ -1346,16 +1340,15 @@ def colors():
1346
1340
Here is an example that creates a pale turquoise title::
1347
1341
1348
1342
title('Is this the best color?', color='#afeeee')
1349
-
1350
1343
"""
1351
- pass
1352
1344
1353
1345
1354
1346
def colormaps ():
1355
1347
"""
1356
1348
Matplotlib provides a number of colormaps, and others can be added using
1357
- :func:`~matplotlib.cm.register_cmap`. This function documents the built-in
1358
- colormaps, and will also return a list of all registered colormaps if called.
1349
+ `~matplotlib.cm.register_cmap`. This function documents the built-in
1350
+ colormaps, and will also return a list of all registered colormaps if
1351
+ called.
1359
1352
1360
1353
You can set the colormap for an image, pcolor, scatter, etc,
1361
1354
using a keyword argument::
@@ -1612,7 +1605,7 @@ def pad(s, l):
1612
1605
exclude = {"colormaps" ,"colors" ,"connect" ,"disconnect" ,
1613
1606
"get_current_fig_manager" ,"ginput" ,"plotting" ,
1614
1607
"waitforbuttonpress" }
1615
- commands = sorted (set (__all__ )- exclude - set (colormaps ()))
1608
+ commands = sorted (set (__all__ )- exclude - set (colormaps ()))
1616
1609
1617
1610
first_sentence = re .compile (r"(?:\s*).+?\.(?:\s+|$)" ,flags = re .DOTALL )
1618
1611
@@ -1660,9 +1653,7 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
1660
1653
'with contourf).' )
1661
1654
if ax is None :
1662
1655
ax = gca ()
1663
-
1664
- ret = gcf ().colorbar (mappable ,cax = cax ,ax = ax ,** kw )
1665
- return ret
1656
+ return gcf ().colorbar (mappable ,cax = cax ,ax = ax ,** kw )
1666
1657
colorbar .__doc__ = matplotlib .colorbar .colorbar_doc
1667
1658
1668
1659
@@ -1727,7 +1718,6 @@ def matshow(A, fignum=None, **kw):
1727
1718
kwarg to "lower" if you want the first row in the array to be
1728
1719
at the bottom instead of the top.
1729
1720
1730
-
1731
1721
*fignum*: [ None | integer | False ]
1732
1722
By default, :func:`matshow` creates a new figure window with
1733
1723
automatic numbering. If *fignum* is given as an integer, the
@@ -1742,9 +1732,9 @@ def matshow(A, fignum=None, **kw):
1742
1732
if fignum is False or fignum is 0 :
1743
1733
ax = gca ()
1744
1734
else :
1745
- # Extract actual aspect ratio of array and make appropriately sized figure
1735
+ # Extractarray's actual aspect ratio; make appropriately sized figure.
1746
1736
fig = figure (fignum ,figsize = figaspect (A ))
1747
- ax = fig .add_axes ([0.15 ,0.09 ,0.775 ,0.775 ])
1737
+ ax = fig .add_axes ([0.15 ,0.09 ,0.775 ,0.775 ])
1748
1738
1749
1739
im = ax .matshow (A ,** kw )
1750
1740
sci (im )