25
25
# `.Axes.plot` to draw some data on the axes:
26
26
27
27
fig , ax = plt .subplots () # Create a figure containing a single axes.
28
- ax .plot ([1 , 2 , 3 , 4 ], [1 , 4 , 2 , 3 ]) # Plot some data on the axes.
29
- plt .show ()
28
+ ax .plot ([1 , 2 , 3 , 4 ], [1 , 4 , 2 , 3 ]); # Plot some data on the axes.
30
29
31
30
###############################################################################
32
31
# .. _figure_parts:
124
123
fig , ax = plt .subplots (figsize = (5 , 2.7 ), constrained_layout = True )
125
124
ax .scatter ('a' , 'b' , c = 'c' , s = 'd' , data = data )
126
125
ax .set_xlabel ('entry a' )
127
- ax .set_ylabel ('entry b' )
128
- plt .show ()
126
+ ax .set_ylabel ('entry b' );
129
127
130
128
##############################################################################
131
129
# .. _coding_styles:
155
153
ax .set_xlabel ('x label' ) # Add an x-label to the axes.
156
154
ax .set_ylabel ('y label' ) # Add a y-label to the axes.
157
155
ax .set_title ("Simple Plot" ) # Add a title to the axes.
158
- ax .legend () # Add a legend.
159
- plt .show ()
156
+ ax .legend (); # Add a legend.
160
157
161
158
###############################################################################
162
159
# or the pyplot-style:
170
167
plt .xlabel ('x label' )
171
168
plt .ylabel ('y label' )
172
169
plt .title ("Simple Plot" )
173
- plt .legend ()
174
- plt .show ()
170
+ plt .legend ();
175
171
176
172
###############################################################################
177
173
# (In addition, there is a third approach, for the case when embedding
182
178
# Matplotlib's documentation and examples use both the OO and the pyplot
183
179
# styles. In general, we suggest using the OO style, particularly for
184
180
# complicated plots, and functions and scripts that are intended to be reused
185
- # as part of a larger project. However, the pyplot style can be very conveneient
186
- # for quick interactive work.
181
+ # as part of a larger project. However, the pyplot style can be very
182
+ # conveneient for quick interactive work.
187
183
#
188
184
# .. note::
189
185
#
193
189
# Making a helper functions
194
190
# -------------------------
195
191
#
196
- # If you need to make the same plots over and over again with different data sets,
197
- # or want to easily wrap Matplotlib methods, use the recommended signature function
198
- # below.
192
+ # If you need to make the same plots over and over again with different data
193
+ # sets, or want to easily wrap Matplotlib methods, use the recommended
194
+ # signature function below.
199
195
200
196
201
197
def my_plotter (ax , data1 , data2 , param_dict ):
@@ -212,8 +208,7 @@ def my_plotter(ax, data1, data2, param_dict):
212
208
xdata = np .arange (len (data1 )) # make an ordinal for this
213
209
fig , (ax1 , ax2 ) = plt .subplots (1 , 2 , figsize = (5 , 2.7 ))
214
210
my_plotter (ax1 , data1 , data2 , {'marker' : 'x' })
215
- my_plotter (ax2 , data3 , data4 , {'marker' : 'o' })
216
- plt .show ()
211
+ my_plotter (ax2 , data3 , data4 , {'marker' : 'o' });
217
212
218
213
###############################################################################
219
214
# Note that if you want to install these as a python pac
628C
kage, or any other
@@ -235,8 +230,7 @@ def my_plotter(ax, data1, data2, param_dict):
235
230
x = np .arange (len (data1 ))
236
231
ax .plot (x , np .cumsum (data1 ), color = 'blue' , linewidth = 3 , linestyle = '--' )
237
232
l , = ax .plot (x , np .cumsum (data2 ), color = 'orange' , linewidth = 2 )
238
- l .set_linestyle (':' )
239
- plt .show ()
233
+ l .set_linestyle (':' );
240
234
241
235
###############################################################################
242
236
# Colors
@@ -250,8 +244,7 @@ def my_plotter(ax, data1, data2, param_dict):
250
244
251
245
fig , ax = plt .subplots (figsize = (5 , 2.7 ))
252
246
x = np .arange (len (data1 ))
253
- ax .scatter (data1 , data2 , s = 50 , facecolor = 'C0' , edgecolor = 'k' )
254
- plt .show ()
247
+ ax .scatter (data1 , data2 , s = 50 , facecolor = 'C0' , edgecolor = 'k' );
255
248
256
249
###############################################################################
257
250
# Linewidths, linestyles, and markersizes
@@ -275,10 +268,9 @@ def my_plotter(ax, data1, data2, param_dict):
275
268
ax .plot (data2 , 'd' , label = 'data2' )
276
269
ax .plot (data3 , 'v' , label = 'data3' )
277
270
ax .plot (data4 , 's' , label = 'data4' )
278
- ax .legend ()
279
- plt .show ()
271
+ ax .legend ();
280
272
281
- ################################################################################
273
+ ###############################################################################
282
274
#
283
275
# Labelling plots
284
276
# ===============
@@ -287,8 +279,8 @@ def my_plotter(ax, data1, data2, param_dict):
287
279
# --------------------
288
280
#
289
281
# `~.Axes.set_xlabel`, `~.Axes.set_ylabel`, and `~.Axes.set_title` are used to
290
- # add text in the indicated locations (see :doc:`/tutorials/text/text_intro` for
291
- # more discussion). Text can also be directly added to plots using
282
+ # add text in the indicated locations (see :doc:`/tutorials/text/text_intro`
283
+ # for more discussion). Text can also be directly added to plots using
292
284
# `~.Axes.text`:
293
285
294
286
mu , sigma = 115 , 15
@@ -302,8 +294,7 @@ def my_plotter(ax, data1, data2, param_dict):
302
294
ax .set_title ('Aardvark lengths\n (not really)' )
303
295
ax .text (75 , .025 , r'$\mu=115,\ \sigma=15$' )
304
296
ax .axis ([55 , 175 , 0 , 0.03 ])
305
- ax .grid (True )
306
- plt .show ()
297
+ ax .grid (True );
307
298
308
299
###############################################################################
309
300
# All of the `~.Axes.text` functions return a `matplotlib.text.Text`
@@ -347,8 +338,7 @@ def my_plotter(ax, data1, data2, param_dict):
347
338
ax .annotate ('local max' , xy = (2 , 1 ), xytext = (3 , 1.5 ),
348
339
arrowprops = dict (facecolor = 'black' , shrink = 0.05 ))
349
340
350
- ax .set_ylim (- 2 , 2 )
351
- plt .show ()
341
+ ax .set_ylim (- 2 , 2 );
352
342
353
343
###############################################################################
354
344
# In this basic example, both *xy* and *xytext* are in data coordinates.
@@ -366,8 +356,7 @@ def my_plotter(ax, data1, data2, param_dict):
366
356
ax .plot (np .arange (len (data1 )), data1 , label = 'data1' )
367
357
ax .plot (np .arange (len (data2 )), data2 , label = 'data2' )
368
358
ax .plot (np .arange (len (data3 )), data3 , 'd' , label = 'data3' )
369
- ax .legend ()
370
- plt .show ()
359
+ ax .legend ();
371
360
372
361
##############################################################################
373
362
# Legends in Matplotlib are quite flexible in layout, placement, and what
@@ -377,9 +366,9 @@ def my_plotter(ax, data1, data2, param_dict):
377
366
# Axis scales and ticks
378
367
# =====================
379
368
#
380
- # Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and y-axis.
381
- # These control the *scale* of the axis, the tick *Locators* and the tick
382
- # *Formatters*.
369
+ # Each Axes has two (or three) `~.axis.Axis` objects represnting the x- and
370
+ # y-axis. These control the *scale* of the axis, the tick *Locators* and the
371
+ # tick *Formatters*.
383
372
#
384
373
# Scales
385
374
# ------
@@ -396,8 +385,7 @@ def my_plotter(ax, data1, data2, param_dict):
396
385
axs [0 ].plot (xdata , data )
397
386
398
387
axs [1 ].set_yscale ('log' )
399
- axs [1 ].plot (xdata , data )
400
- plt .show ()
388
+ axs [1 ].plot (xdata , data );
401
389
402
390
##############################################################################
403
391
# The scale sets the mapping from data values to spacing along the Axis. This
@@ -418,8 +406,7 @@ def my_plotter(ax, data1, data2, param_dict):
418
406
axs [1 ].plot (xdata , data1 )
419
407
axs [1 ].set_xticks (np .arange (0 , 100 , 30 ), ['zero' , '30' , 'sixty' , '90' ])
420
408
axs [1 ].set_yticks ([- 1.5 , 0 , 1.5 ]) # note that we don't need to specify labels
421
- axs [1 ].set_title ('Manual ticks' )
422
- plt .show ()
409
+ axs [1 ].set_title ('Manual ticks' );
423
410
424
411
##############################################################################
425
412
# Different scales can have different locators and formatters; for instance
@@ -439,8 +426,7 @@ def my_plotter(ax, data1, data2, param_dict):
439
426
dates = np .arange (np .datetime64 ('2021-11-15' ), np .datetime64 ('2021-12-25' ),
440
427
np .timedelta64 (1 , 'h' ))
441
428
data = np .cumsum (np .random .randn (len (dates )))
442
- ax .plot (dates , data )
443
- plt .show ()
429
+ ax .plot (dates , data );
444
430
445
431
##############################################################################
446
432
# For more information see the date examples
@@ -452,8 +438,7 @@ def my_plotter(ax, data1, data2, param_dict):
452
438
fig , ax = plt .subplots (figsize = (5 , 2.7 ), constrained_layout = True )
453
439
categories = ['turnips' , 'rutabega' , 'cucumber' , 'pumpkins' ]
454
440
455
- ax .bar (categories , np .random .rand (len (categories )))
456
- plt .show ()
441
+ ax .bar (categories , np .random .rand (len (categories )));
457
442
458
443
##############################################################################
459
444
# One caveat about categorical plotting is that some methods of parsing
@@ -486,8 +471,7 @@ def my_plotter(ax, data1, data2, param_dict):
486
471
487
472
pc = axs [1 , 1 ].scatter (data1 , data2 , c = data3 , cmap = 'RdBu_r' )
488
473
fig .colorbar (pc , ax = axs [1 , 1 ], extend = 'both' )
489
- axs [1 , 1 ].set_title ('scatter()' )
490
- plt .show ()
474
+ axs [1 , 1 ].set_title ('scatter()' );
491
475
492
476
##############################################################################
493
477
# Colormaps
0 commit comments