Skip to content

Commit ff9e06a

Browse files
authored
Merge pull request #321 from GParolini/oo-matplotlib
Episodes modified using Matplotlib Object Oriented Style
2 parents efa451b + 193f8ea commit ff9e06a

8 files changed

+143
-147
lines changed

episodes/02-image-basics.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ With that taken care of, let us display the image we have loaded, using
228228
the `imshow` function from the `matplotlib.pyplot` module.
229229

230230
```python
231-
plt.imshow(eight)
231+
fig, ax = plt.subplots()
232+
ax.imshow(eight)
232233
```
233234

234235
![](fig/eight.png){alt='Image of 8'}
@@ -289,9 +290,8 @@ zero = iio.imread(uri="data/eight.tif")
289290
zero[2,1]= 1.0
290291

291292
# The following line of code creates a new figure for imshow to use in displaying our output.
292-
# Without it, plt.imshow() would overwrite our previous image in the cell above
293293
fig, ax = plt.subplots()
294-
plt.imshow(zero)
294+
ax.imshow(zero)
295295
print(zero)
296296
```
297297

@@ -365,7 +365,7 @@ five = iio.imread(uri="data/eight.tif")
365365
five[1,2]= 1.0
366366
five[3,0]= 1.0
367367
fig, ax = plt.subplots()
368-
plt.imshow(five)
368+
ax.imshow(five)
369369
print(five)
370370
```
371371

@@ -402,7 +402,7 @@ three_colours = three_colours * 128
402402
# so you end up with the values 0., 128., and 255.
403403
three_colours[2,:] = 255.
404404
fig, ax = plt.subplots()
405-
plt.imshow(three_colours)
405+
ax.imshow(three_colours)
406406
print(three_colours)
407407
```
408408

@@ -436,7 +436,7 @@ a mapped continuum of intensities: greyscale.
436436

437437
```python
438438
fig, ax = plt.subplots()
439-
plt.imshow(three_colours,cmap=plt.cm.gray)
439+
ax.imshow(three_colours,cmap=plt.cm.gray)
440440
```
441441

442442
![](fig/grayscale.png){alt='Image in greyscale'}
@@ -472,7 +472,7 @@ pseudorandomizer = np.random.RandomState(2021)
472472
checkerboard = pseudorandomizer.randint(0, 255, size=(4, 4, 3))
473473
# restore the default map as you show the image
474474
fig, ax = plt.subplots()
475-
plt.imshow(checkerboard)
475+
ax.imshow(checkerboard)
476476
# display the arrays
477477
print(checkerboard)
478478
```
@@ -535,23 +535,23 @@ a 1d matrix that has a one for the channel we want to keep and zeros for the res
535535
```python
536536
red_channel = checkerboard * [1, 0, 0]
537537
fig, ax = plt.subplots()
538-
plt.imshow(red_channel)
538+
ax.imshow(red_channel)
539539
```
540540

541541
![](fig/checkerboard-red-channel.png){alt='Image of red channel'}
542542

543543
```python
544544
green_channel = checkerboard * [0, 1, 0]
545545
fig, ax = plt.subplots()
546-
plt.imshow(green_channel)
546+
ax.imshow(green_channel)
547547
```
548548

549549
![](fig/checkerboard-green-channel.png){alt='Image of green channel'}
550550

551551
```python
552552
blue_channel = checkerboard * [0, 0, 1]
553553
fig, ax = plt.subplots()
554-
plt.imshow(blue_channel)
554+
ax.imshow(blue_channel)
555555
```
556556

557557
![](fig/checkerboard-blue-channel.png){alt='Image of blue channel'}

episodes/03-skimage-images.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ Next, we will do something with the image:
6060

6161
```python
6262
fig, ax = plt.subplots()
63-
plt.imshow(chair)
63+
ax.imshow(chair)
6464
```
6565

6666
Once we have the image in the program,
67-
we first call `plt.subplots()` so that we will have
68-
a fresh figure with a set of axis independent from our previous calls.
69-
Next we call `plt.imshow()` in order to display the image.
67+
we first call `fig, ax = plt.subplots()` so that we will have
68+
a fresh figure with a set of axes independent from our previous calls.
69+
Next we call `ax.imshow()` in order to display the image.
7070

7171
Now, we will save the image in another format:
7272

@@ -181,7 +181,7 @@ If we don't convert it before saving,
181181

182182
Next, write the resized image out to a new file named `resized.jpg`
183183
in your data directory.
184-
Finally, use `plt.imshow()` with each of your image variables to display
184+
Finally, use `ax.imshow()` with each of your image variables to display
185185
both images in your notebook.
186186
Don't forget to use `fig, ax = plt.subplots()` so you don't overwrite
187187
the first image with the second.
@@ -214,9 +214,9 @@ iio.imwrite(uri="data/resized_chair.jpg", image=resized_chair)
214214

215215
# display images
216216
fig, ax = plt.subplots()
217-
plt.imshow(chair)
217+
ax.imshow(chair)
218218
fig, ax = plt.subplots()
219-
plt.imshow(resized_chair)
219+
ax.imshow(resized_chair)
220220
```
221221

222222
The script resizes the `data/chair.jpg` image by a factor of 10 in both dimensions,
@@ -271,7 +271,7 @@ maize_roots = np.array(maize_roots)
271271

272272
# display original image
273273
fig, ax = plt.subplots()
274-
plt.imshow(maize_roots)
274+
ax.imshow(maize_roots)
275275
```
276276

277277
Now we can threshold the image and display the result.
@@ -282,7 +282,7 @@ maize_roots[maize_roots < 128] = 0
282282

283283
# display modified image
284284
fig, ax = plt.subplots()
285-
plt.imshow(maize_roots)
285+
ax.imshow(maize_roots)
286286
```
287287

288288
The NumPy command to ignore all low-intensity pixels is `roots[roots < 128] = 0`.
@@ -331,12 +331,12 @@ chair = iio.imread(uri="data/chair.jpg")
331331

332332
# display original image
333333
fig, ax = plt.subplots()
334-
plt.imshow(chair)
334+
ax.imshow(chair)
335335

336336
# convert to grayscale and display
337337
gray_chair = ski.color.rgb2gray(chair)
338338
fig, ax = plt.subplots()
339-
plt.imshow(gray_chair, cmap="gray")
339+
ax.imshow(gray_chair, cmap="gray")
340340
```
341341

342342
We can also load colour images as grayscale directly by
@@ -350,7 +350,7 @@ gray_chair = iio.imread(uri="data/chair.jpg", mode="L")
350350

351351
# display grayscale image
352352
fig, ax = plt.subplots()
353-
plt.imshow(gray_chair, cmap="gray")
353+
ax.imshow(gray_chair, cmap="gray")
354354
```
355355

356356
The first argument to `iio.imread()` is the filename of the image.
@@ -415,7 +415,6 @@ sudoku_gray_background[sudoku_gray_background > 192] = 192
415415
Finally, display the original and modified images side by side. Note that we have to specify `vmin=0` and `vmax=255` as the range of the colorscale because it would otherwise automatically adjust to the new range 0-192.
416416

417417
```python
418-
fig, ax = plt.subplots()
419418
fig, ax = plt.subplots(ncols=2)
420419
ax[0].imshow(sudoku, cmap="gray", vmin=0, vmax=255)
421420
ax[1].imshow(sudoku_gray_background, cmap="gray", vmin=0, vmax=255)
@@ -430,11 +429,11 @@ ax[1].imshow(sudoku_gray_background, cmap="gray", vmin=0, vmax=255)
430429
## Plotting single channel images (cmap, vmin, vmax)
431430

432431
Compared to a colour image, a grayscale image contains only a single
433-
intensity value per pixel. When we plot such an image with `plt.imshow`,
432+
intensity value per pixel. When we plot such an image with `ax.imshow`,
434433
Matplotlib uses a colour map, to assign each intensity value a colour.
435434
The default colour map is called "viridis" and maps low values to purple
436435
and high values to yellow. We can instruct Matplotlib to map low values
437-
to black and high values to white instead, by calling `plt.imshow` with
436+
to black and high values to white instead, by calling `ax.imshow` with
438437
`cmap="gray"`.
439438
[The documentation contains an overview of pre-defined colour maps](https://matplotlib.org/stable/gallery/color/colormap_reference.html).
440439

@@ -499,7 +498,7 @@ A script to create the subimage would start by loading the image:
499498
board = iio.imread(uri="data/board.jpg")
500499
board = np.array(board)
501500
fig, ax = plt.subplots()
502-
plt.imshow(board)
501+
ax.imshow(board)
503502
```
504503

505504
Then we use array slicing to
@@ -509,7 +508,7 @@ create a new image with our selected area and then display the new image.
509508
# extract, display, and save sub-image
510509
clipped_board = board[60:151, 135:481, :]
511510
fig, ax = plt.subplots()
512-
plt.imshow(clipped_board)
511+
ax.imshow(clipped_board)
513512
iio.imwrite(uri="data/clipped_board.tif", image=clipped_board)
514513
```
515514

@@ -520,7 +519,7 @@ We can also change the values in an image, as shown next.
520519
color = board[330, 90]
521520
board[60:151, 135:481] = color
522521
fig, ax = plt.subplots()
523-
plt.imshow(board)
522+
ax.imshow(board)
524523
```
525524

526525
First, we sample a single pixel's colour at a particular location of the
@@ -559,12 +558,12 @@ in the image.
559558
# load and display original image
560559
maize_roots = iio.imread(uri="data/maize-root-cluster.jpg")
561560
fig, ax = plt.subplots()
562-
plt.imshow(maize_roots)
561+
ax.imshow(maize_roots)
563562

564563
# extract and display sub-image
565564
clipped_maize = maize_roots[0:400, 275:550, :]
566565
fig, ax = plt.subplots()
567-
plt.imshow(clipped_maize)
566+
ax.imshow(clipped_maize)
568567

569568

570569
# save sub-image

episodes/04-drawing.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ image:
7777
maize_seedlings = iio.imread(uri="data/maize-seedlings.tif")
7878

7979
fig, ax = plt.subplots()
80-
plt.imshow(maize_seedlings)
80+
ax.imshow(maize_seedlings)
8181
```
8282

8383
We load and display the initial image in the same way we have done before.
@@ -117,7 +117,7 @@ mask[rr, cc] = False
117117

118118
# Display mask image
119119
fig, ax = plt.subplots()
120-
plt.imshow(mask, cmap="gray")
120+
ax.imshow(mask, cmap="gray")
121121
```
122122

123123
Here is what our constructed mask looks like:
@@ -229,7 +229,7 @@ canvas[rr, cc] = (0, 255, 0)
229229
```python
230230
# Display the image
231231
fig, ax = plt.subplots()
232-
plt.imshow(canvas)
232+
ax.imshow(canvas)
233233
```
234234

235235
We could expand this solution, if we wanted,
@@ -258,7 +258,7 @@ for i in range(15):
258258

259259
# display the results
260260
fig, ax = plt.subplots()
261-
plt.imshow(canvas)
261+
ax.imshow(canvas)
262262
```
263263

264264
We could expand this even further to also
@@ -306,7 +306,7 @@ for i in range(15):
306306

307307
# display the results
308308
fig, ax = plt.subplots()
309-
plt.imshow(canvas)
309+
ax.imshow(canvas)
310310
```
311311

312312
:::::::::::::::::::::::::
@@ -374,7 +374,7 @@ Then, we display the masked image.
374374

375375
```python
376376
fig, ax = plt.subplots()
377-
plt.imshow(maize_seedlings)
377+
ax.imshow(maize_seedlings)
378378
```
379379

380380
The resulting masked image should look like this:
@@ -423,7 +423,7 @@ remote[mask] = 0
423423

424424
# Display the result
425425
fig, ax = plt.subplots()
426-
plt.imshow(remote)
426+
ax.imshow(remote)
427427
```
428428

429429
:::::::::::::::::::::::::
@@ -443,7 +443,7 @@ wellplate = np.array(wellplate)
443443

444444
# Display the image
445445
fig, ax = plt.subplots()
446-
plt.imshow(wellplate)
446+
ax.imshow(wellplate)
447447
```
448448

449449
![](data/wellplate-01.jpg){alt='96-well plate'}
@@ -491,7 +491,7 @@ wellplate[mask] = 0
491491

492492
# display the result
493493
fig, ax = plt.subplots()
494-
plt.imshow(wellplate)
494+
ax.imshow(wellplate)
495495
```
496496

497497
:::::::::::::::::::::::::
@@ -564,7 +564,7 @@ wellplate[mask] = 0
564564

565565
# display the result
566566
fig, ax = plt.subplots()
567-
plt.imshow(wellplate)
567+
ax.imshow(wellplate)
568568
```
569569

570570
:::::::::::::::::::::::::

0 commit comments

Comments
 (0)