Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Episodes modified using Matplotlib Object Oriented Style #321

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions episodes/02-image-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ With that taken care of, let us display the image we have loaded, using
the `imshow` function from the `matplotlib.pyplot` module.

```python
plt.imshow(eight)
fig, ax = plt.subplots()
ax.imshow(eight)
```

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

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

Expand Down Expand Up @@ -365,7 +365,7 @@ five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
ax.imshow(five)
print(five)
```

Expand Down Expand Up @@ -402,7 +402,7 @@ three_colours = three_colours * 128
# so you end up with the values 0., 128., and 255.
three_colours[2,:] = 255.
fig, ax = plt.subplots()
plt.imshow(three_colours)
ax.imshow(three_colours)
print(three_colours)
```

Expand Down Expand Up @@ -436,7 +436,7 @@ a mapped continuum of intensities: greyscale.

```python
fig, ax = plt.subplots()
plt.imshow(three_colours,cmap=plt.cm.gray)
ax.imshow(three_colours,cmap=plt.cm.gray)
```

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

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

```python
green_channel = checkerboard * [0, 1, 0]
fig, ax = plt.subplots()
plt.imshow(green_channel)
ax.imshow(green_channel)
```

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

```python
blue_channel = checkerboard * [0, 0, 1]
fig, ax = plt.subplots()
plt.imshow(blue_channel)
ax.imshow(blue_channel)
```

![](fig/checkerboard-blue-channel.png){alt='Image of blue channel'}
Expand Down
39 changes: 19 additions & 20 deletions episodes/03-skimage-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ Next, we will do something with the image:

```python
fig, ax = plt.subplots()
plt.imshow(chair)
ax.imshow(chair)
```

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

Now, we will save the image in another format:

Expand Down Expand Up @@ -181,7 +181,7 @@ If we don't convert it before saving,

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

# display images
fig, ax = plt.subplots()
plt.imshow(chair)
ax.imshow(chair)
fig, ax = plt.subplots()
plt.imshow(resized_chair)
ax.imshow(resized_chair)
```

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

# display original image
fig, ax = plt.subplots()
plt.imshow(maize_roots)
ax.imshow(maize_roots)
```

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

# display modified image
fig, ax = plt.subplots()
plt.imshow(maize_roots)
ax.imshow(maize_roots)
```

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

# display original image
fig, ax = plt.subplots()
plt.imshow(chair)
ax.imshow(chair)

# convert to grayscale and display
gray_chair = ski.color.rgb2gray(chair)
fig, ax = plt.subplots()
plt.imshow(gray_chair, cmap="gray")
ax.imshow(gray_chair, cmap="gray")
```

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

# display grayscale image
fig, ax = plt.subplots()
plt.imshow(gray_chair, cmap="gray")
ax.imshow(gray_chair, cmap="gray")
```

The first argument to `iio.imread()` is the filename of the image.
Expand Down Expand Up @@ -415,7 +415,6 @@ sudoku_gray_background[sudoku_gray_background > 192] = 192
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.

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

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

Expand Down Expand Up @@ -499,7 +498,7 @@ A script to create the subimage would start by loading the image:
board = iio.imread(uri="data/board.jpg")
board = np.array(board)
fig, ax = plt.subplots()
plt.imshow(board)
ax.imshow(board)
```

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

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

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

# extract and display sub-image
clipped_maize = maize_roots[0:400, 275:550, :]
fig, ax = plt.subplots()
plt.imshow(clipped_maize)
ax.imshow(clipped_maize)


# save sub-image
Expand Down
20 changes: 10 additions & 10 deletions episodes/04-drawing.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ image:
maize_seedlings = iio.imread(uri="data/maize-seedlings.tif")

fig, ax = plt.subplots()
plt.imshow(maize_seedlings)
ax.imshow(maize_seedlings)
```

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

# Display mask image
fig, ax = plt.subplots()
plt.imshow(mask, cmap="gray")
ax.imshow(mask, cmap="gray")
```

Here is what our constructed mask looks like:
Expand Down Expand Up @@ -229,7 +229,7 @@ canvas[rr, cc] = (0, 255, 0)
```python
# Display the image
fig, ax = plt.subplots()
plt.imshow(canvas)
ax.imshow(canvas)
```

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

# display the results
fig, ax = plt.subplots()
plt.imshow(canvas)
ax.imshow(canvas)
```

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

# display the results
fig, ax = plt.subplots()
plt.imshow(canvas)
ax.imshow(canvas)
```

:::::::::::::::::::::::::
Expand Down Expand Up @@ -374,7 +374,7 @@ Then, we display the masked image.

```python
fig, ax = plt.subplots()
plt.imshow(maize_seedlings)
ax.imshow(maize_seedlings)
```

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

# Display the result
fig, ax = plt.subplots()
plt.imshow(remote)
ax.imshow(remote)
```

:::::::::::::::::::::::::
Expand All @@ -443,7 +443,7 @@ wellplate = np.array(wellplate)

# Display the image
fig, ax = plt.subplots()
plt.imshow(wellplate)
ax.imshow(wellplate)
```

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

# display the result
fig, ax = plt.subplots()
plt.imshow(wellplate)
ax.imshow(wellplate)
```

:::::::::::::::::::::::::
Expand Down Expand Up @@ -564,7 +564,7 @@ wellplate[mask] = 0

# display the result
fig, ax = plt.subplots()
plt.imshow(wellplate)
ax.imshow(wellplate)
```

:::::::::::::::::::::::::
Expand Down
Loading
Loading