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.
@@ -736,12 +734,13 @@ def subplot(*args, **kwargs):
736
734
import matplotlib.pyplot as plt
737
735
# plot a line, implicitly creating a subplot(111)
738
736
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
737
+ # now create a subplot which represents the top plot of a grid with
738
+ # 2 rows and 1 column. Since this subplot will overlap the first, the
739
+ # plot (and its axes) previously created, will be removed
742
740
plt.subplot(211)
743
741
plt.plot(range(12))
744
- plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background
742
+ # create a second subplot with yellow background
743
+ plt.subplot(212, facecolor='y')
745
744
746
745
If you do not want this behavior, use the
747
746
:meth:`~matplotlib.figure.Figure.add_subplot` method or the
@@ -778,28 +777,26 @@ def subplot(*args, **kwargs):
778
777
779
778
"""
780
779
# if subplot called without arguments, create subplot(1,1,1)
781
- if len (args )== 0 :
782
- args = (1 ,1 , 1 )
780
+ if len (args )== 0 :
781
+ args = (1 ,1 , 1 )
783
782
784
783
# This check was added because it is very easy to type
785
784
# subplot(1, 2, False) when subplots(1, 2, False) was intended
786
785
# (sharex=False, that is). In most cases, no error will
787
786
# ever occur, but mysterious behavior can result because what was
788
787
# intended to be the sharex argument is instead treated as a
789
788
# 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()?" )
789
+ if len (args )>= 3 and isinstance (args [2 ],bool ):
790
+ warnings .warn ("The subplot index argument to subplot() appears "
791
+ "to be a boolean. Did you intend to use subplots()?" )
793
792
794
793
fig = gcf ()
795
794
a = fig .add_subplot (* args ,** kwargs )
796
795
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 )
796
+ byebye = [other for other in fig .axes
797
+ if other is not a and bbox .fully_overlaps (other .bbox )]
798
+ for ax in byebye :
799
+ delaxes (ax )
803
800
804
801
return a
805
802
@@ -1021,24 +1018,28 @@ def subplot_tool(targetfig=None):
1021
1018
"""
1022
1019
Launch a subplot tool window for a figure.
1023
1020
1024
- A :class:`matplotlib.widgets.SubplotTool` instance is returned.
1021
+ Returns
1022
+ -------
1023
+ `matplotlib.widgets.SubplotTool`
1025
1024
"""
1026
- tbar = rcParams [' toolbar' ] # turn off thenavigation toolbar for the toolfig
1027
- rcParams [' toolbar' ]= ' None'
1025
+ tbar = rcParams [" toolbar" ] # Turn off thenav toolbar for the toolfig.
1026
+ rcParams [" toolbar" ]= " None"
1028
1027
if targetfig is None :
1029
1028
manager = get_current_fig_manager ()
1030
1029
targetfig = manager .canvas .figure
1031
1030
else :
1032
- #find the manager for this figure
1031
+ #Find the manager for this figure.
1033
1032
for manager in _pylab_helpers .Gcf ._activeQue :
1034
- if manager .canvas .figure == targetfig :break
1035
- else :raise RuntimeError ('Could not find manager for targetfig' )
1033
+ if manager .canvas .figure == targetfig :
1034
+ break
1035
+ else :
1036
+ raise RuntimeError ("Could not find manager for targetfig" )
1036
1037
1037
- toolfig = figure (figsize = (6 ,3 ))
1038
+ toolfig = figure (figsize = (6 ,3 ))
1038
1039
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
1040
+ ret = SubplotTool (targetfig ,toolfig )
1041
+ rcParams [" toolbar" ]= tbar
1042
+ _pylab_helpers .Gcf .set_active (manager )#Restore the current figure.
1042
1043
return ret
1043
1044
1044
1045
@@ -1057,10 +1058,8 @@ def box(on=None):
1057
1058
on = not ax .get_frame_on ()
1058
1059
ax .set_frame_on (on )
1059
1060
1060
-
1061
1061
## Axis ##
1062
1062
1063
-
1064
1063
def xlim (* args ,** kwargs ):
1065
1064
"""
1066
1065
Get or set the *x* limits of the current axes.
@@ -1225,15 +1224,14 @@ def rgrids(*args, **kwargs):
1225
1224
"""
1226
1225
ax = gca ()
1227
1226
if not isinstance (ax ,PolarAxes ):
1228
- raise RuntimeError (' rgrids only defined for polar axes' )
1229
- if len (args )== 0 :
1227
+ raise RuntimeError (" rgrids only defined for polar axes" )
1228
+ if len (args )== 0 :
1230
1229
lines = ax .yaxis .get_gridlines ()
1231
1230
labels = ax .yaxis .get_ticklabels ()
1232
1231
else :
1233
1232
lines ,labels = ax .set_rgrids (* args ,** kwargs )
1234
-
1235
- return (silent_list ('Line2D rgridline' ,lines ),
1236
- silent_list ('Text rgridlabel' ,labels ) )
1233
+ return (silent_list ("Line2D rgridline" ,lines ),
1234
+ silent_list ("Text rgridlabel" ,labels ))
1237
1235
1238
1236
1239
1237
def thetagrids (* args ,** kwargs ):
@@ -1271,31 +1269,27 @@ def thetagrids(*args, **kwargs):
1271
1269
1272
1270
- *labels* are :class:`~matplotlib.text.Text` instances.
1273
1271
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.
1272
+ Note that on input, the *labels* argument is a list of strings, and on
1273
+ output it is a list of :class:`~matplotlib.text.Text` instances.
1277
1274
1278
1275
Examples::
1279
1276
1280
1277
# set the locations of the radial gridlines and labels
1281
- lines, labels = thetagrids( range(45,360,90) )
1278
+ lines, labels = thetagrids(range(45, 360, 90))
1282
1279
1283
1280
# set the locations and labels of the radial gridlines and labels
1284
- lines, labels = thetagrids( range(45,360,90), ('NE', 'NW', 'SW','SE') )
1281
+ lines, labels = thetagrids(range(45, 360, 90), ('NE', 'NW', 'SW', 'SE'))
1285
1282
"""
1286
1283
ax = gca ()
1287
1284
if not isinstance (ax ,PolarAxes ):
1288
- raise RuntimeError (' rgrids only defined for polar axes' )
1289
- if len (args )== 0 :
1285
+ raise RuntimeError (" rgrids only defined for polar axes" )
1286
+ if len (args )== 0 :
1290
1287
lines = ax .xaxis .get_ticklines ()
1291
1288
labels = ax .xaxis .get_ticklabels ()
1292
1289
else :
1293
1290
lines ,labels = ax .set_thetagrids (* args ,** kwargs )
1294
-
1295
- return (silent_list ('Line2D thetagridline' ,lines ),
1296
- silent_list ('Text thetagridlabel' ,labels )
1297
- )
1298
-
1291
+ return (silent_list ("Line2D thetagridline" ,lines ),
1292
+ silent_list ("Text thetagridlabel" ,labels ))
1299
1293
1300
1294
## Plotting Info ##
1301
1295
@@ -1362,16 +1356,15 @@ def colors():
1362
1356
Here is an example that creates a pale turquoise title::
1363
1357
1364
1358
title('Is this the best color?', color='#afeeee')
1365
-
1366
1359
"""
1367
- pass
1368
1360
1369
1361
1370
1362
def colormaps ():
1371
1363
"""
1372
1364
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.
1365
+ `~matplotlib.cm.register_cmap`. This function documents the built-in
1366
+ colormaps, and will also return a list of all registered colormaps if
1367
+ called.
1375
1368
1376
1369
You can set the colormap for an image, pcolor, scatter, etc,
1377
1370
using a keyword argument::
@@ -1628,7 +1621,7 @@ def pad(s, l):
1628
1621
exclude = {"colormaps" ,"colors" ,"connect" ,"disconnect" ,
1629
1622
"get_current_fig_manager" ,"ginput" ,"plotting" ,
1630
1623
"waitforbuttonpress" }
1631
- commands = sorted (set (__all__ )- exclude - set (colormaps ()))
1624
+ commands = sorted (set (__all__ )- exclude - set (colormaps ()))
1632
1625
1633
1626
first_sentence = re .compile (r"(?:\s*).+?\.(?:\s+|$)" ,flags = re .DOTALL )
1634
1627
@@ -1676,9 +1669,7 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
1676
1669
'with contourf).' )
1677
1670
if ax is None :
1678
1671
ax = gca ()
1679
-
1680
- ret = gcf ().colorbar (mappable ,cax = cax ,ax = ax ,** kw )
1681
- return ret
1672
+ return gcf ().colorbar (mappable ,cax = cax ,ax = ax ,** kw )
1682
1673
colorbar .__doc__ = matplotlib .colorbar .colorbar_doc
1683
1674
1684
1675
@@ -1743,7 +1734,6 @@ def matshow(A, fignum=None, **kw):
1743
1734
kwarg to "lower" if you want the first row in the array to be
1744
1735
at the bottom instead of the top.
1745
1736
1746
-
1747
1737
*fignum*: [ None | integer | False ]
1748
1738
By default, :func:`matshow` creates a new figure window with
1749
1739
automatic numbering. If *fignum* is given as an integer, the
@@ -1758,9 +1748,9 @@ def matshow(A, fignum=None, **kw):
1758
1748
if fignum is False or fignum is 0 :
1759
1749
ax = gca ()
1760
1750
else :
1761
- # Extract actual aspect ratio of array and make appropriately sized figure
1751
+ # Extractarray's actual aspect ratio; make appropriately sized figure.
1762
1752
fig = figure (fignum ,figsize = figaspect (A ))
1763
- ax = fig .add_axes ([0.15 ,0.09 ,0.775 ,0.775 ])
1753
+ ax = fig .add_axes ([0.15 ,0.09 ,0.775 ,0.775 ])
1764
1754
1765
1755
im = ax .matshow (A ,** kw )
1766
1756
sci (im )