Skip to content

Commit c5fa35a

Browse files
committed
MAINT: Update svd tutorial plt patterns.
1 parent 60c5496 commit c5fa35a

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

content/tutorial-svd.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ import matplotlib.pyplot as plt
7474
```
7575

7676
```{code-cell}
77-
plt.imshow(img)
78-
plt.show()
77+
fig, ax = plt.subplots()
78+
ax.imshow(img)
7979
```
8080

8181
### Shape, axis and array properties
@@ -196,8 +196,8 @@ To see if this makes sense in our image, we should use a colormap from `matplotl
196196
In our case, we are approximating the grayscale portion of the image, so we will use the colormap `gray`:
197197

198198
```{code-cell}
199-
plt.imshow(img_gray, cmap="gray")
200-
plt.show()
199+
fig, ax = plt.subplots()
200+
ax.imshow(img_gray, cmap="gray")
201201
```
202202

203203
Now, applying the [linalg.svd](https://numpy.org/devdocs/reference/generated/numpy.linalg.svd.html#numpy.linalg.svd) function to this matrix, we obtain the following decomposition:
@@ -259,8 +259,8 @@ np.allclose(img_gray, U @ Sigma @ Vt)
259259
To see if an approximation is reasonable, we can check the values in `s`:
260260

261261
```{code-cell}
262-
plt.plot(s)
263-
plt.show()
262+
fig, ax = plt.subplots()
263+
ax.plot(s)
264264
```
265265

266266
In the graph, we can see that although we have 768 singular values in `s`, most of those (after the 150th entry or so) are pretty small. So it might make sense to use only the information related to the first (say, 50) *singular values* to build a more economical approximation to our image.
@@ -282,8 +282,8 @@ approx = U @ Sigma[:, :k] @ Vt[:k, :]
282282
Note that we had to use only the first `k` rows of `Vt`, since all other rows would be multiplied by the zeros corresponding to the singular values we eliminated from this approximation.
283283

284284
```{code-cell}
285-
plt.imshow(approx, cmap="gray")
286-
plt.show()
285+
fig, ax = plt.subplots()
286+
ax.imshow(approx, cmap="gray")
287287
```
288288

289289
Now, you can go ahead and repeat this experiment with other values of `k`, and each of your experiments should give you a slightly better (or worse) image depending on the value you choose.
@@ -362,8 +362,9 @@ Since `imshow` expects values in the range, we can use `clip` to excise the floa
362362

363363
```{code-cell}
364364
reconstructed = np.clip(reconstructed, 0, 1)
365-
plt.imshow(np.transpose(reconstructed, (1, 2, 0)))
366-
plt.show()
365+
366+
fig, ax = plt.subplots()
367+
ax.imshow(np.transpose(reconstructed, (1, 2, 0)))
367368
```
368369

369370
```{note}
@@ -391,8 +392,8 @@ approx_img.shape
391392
which is not the right shape for showing the image. Finally, reordering the axes back to our original shape of `(768, 1024, 3)`, we can see our approximation:
392393

393394
```{code-cell}
394-
plt.imshow(np.transpose(np.clip(approx_img, 0, 1), (1, 2, 0)))
395-
plt.show()
395+
fig, ax = plt.subplots()
396+
ax.imshow(np.transpose(np.clip(approx_img, 0, 1), (1, 2, 0)))
396397
```
397398

398399
Even though the image is not as sharp, using a small number of `k` singular values (compared to the original set of 768 values), we can recover many of the distinguishing features from this image.

0 commit comments

Comments
 (0)