113
113
# `numpy.recarray`, or a `pandas.DataFrame`. Matplotlib allows you provide
114
114
# the ``data`` keyword argument and generate plots passing the strings
115
115
# corresponding to the *x* and *y* variables.
116
- np .random .seed (19680801 )
116
+ np .random .seed (19680801 ) # seed the random number generator.
117
117
data = {'a' : np .arange (50 ),
118
118
'c' : np .random .randint (0 , 50 , 50 ),
119
119
'd' : np .random .randn (50 )}
120
120
data ['b' ] = data ['a' ] + 10 * np .random .randn (50 )
121
121
data ['d' ] = np .abs (data ['d' ]) * 100
122
122
123
- fig , ax = plt .subplots (figsize = (5 , 2.7 ))
123
+ fig , ax = plt .subplots (figsize = (5 , 2.7 ), constrained_layout = True )
124
124
ax .scatter ('a' , 'b' , c = 'c' , s = 'd' , data = data )
125
125
ax .set_xlabel ('entry a' )
126
126
ax .set_ylabel ('entry b' )
146
146
x = np .linspace (0 , 2 , 100 ) # Sample data.
147
147
148
148
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
149
- fig , ax = plt .subplots (figsize = (5 , 2.7 )) # Create a figure and an axes.
149
+ fig , ax = plt .subplots (figsize = (5 , 2.7 ), constrained_layout = True )
150
150
ax .plot (x , x , label = 'linear' ) # Plot some data on the axes.
151
151
ax .plot (x , x ** 2 , label = 'quadratic' ) # Plot more data on the axes...
152
152
ax .plot (x , x ** 3 , label = 'cubic' ) # ... and some more.
160
160
161
161
x = np .linspace (0 , 2 , 100 ) # Sample data.
162
162
163
- plt .figure (figsize = (5 , 2.7 ))
163
+ plt .figure (figsize = (5 , 2.7 ), constrained_layout = True )
164
164
plt .plot (x , x , label = 'linear' ) # Plot some data on the (implicit) axes.
165
165
plt .plot (x , x ** 2 , label = 'quadratic' ) # etc.
166
166
plt .plot (x , x ** 3 , label = 'cubic' )
@@ -287,7 +287,7 @@ def my_plotter(ax, data1, data2, param_dict):
287
287
288
288
mu , sigma = 100 , 15
289
289
x = mu + sigma * np .random .randn (10000 )
290
- fig , ax = plt .subplots (figsize = (5 , 2.7 ))
290
+ fig , ax = plt .subplots (figsize = (5 , 2.7 ), constrained_layout = True )
291
291
# the histogram of the data
292
292
n , bins , patches = ax .hist (x , 50 , density = 1 , facecolor = 'C0' , alpha = 0.75 )
293
293
@@ -297,6 +297,7 @@ def my_plotter(ax, data1, data2, param_dict):
297
297
ax .text (60 , .025 , r'$\mu=100,\ \sigma=15$' )
298
298
ax .axis ([40 , 160 , 0 , 0.03 ])
299
299
ax .grid (True )
300
+ plt .show ()
300
301
301
302
###############################################################################
302
303
# All of the `~.Axes.text` functions return a `matplotlib.text.Text`
@@ -414,7 +415,7 @@ def my_plotter(ax, data1, data2, param_dict):
414
415
# Different scales can have different locators and formatters; for instance
415
416
# the log-scale above uses `~.LogLocator` and `~.LogFormatter`. See
416
417
# :doc:`/gallery/ticks/tick-locators` and
417
- # :doc:gallery/ticks/tick-formatters` for other formatters and
418
+ # :doc:`/ gallery/ticks/tick-formatters` for other formatters and
418
419
# locators and information for writing your own.
419
420
#
420
421
# Plotting dates and strings
0 commit comments