Skip to content

Commit

Permalink
Enhance data visualization examples with 1D, 2D, and 3D plotting; add…
Browse files Browse the repository at this point in the history
… image manipulation techniques and examples
  • Loading branch information
Cotswoldsmaker committed Nov 6, 2024
1 parent e36016e commit 6a36eee
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 5 deletions.
92 changes: 90 additions & 2 deletions learn/learn-python/module-3/4-displaying-data.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ import matplotlib.pyplot as plot
array_1x7 = np.array([1, 2, 2, 4, 1, 1, 7])
plot.plot(array_1x7)
plot.show()
```

## Let's plot some 1D data

![](/media/1d-plot-pyplot-basic.png){.centre-full-image .top-55}


## Let's plot some 1D data

```{.python filename="pyplot_1d.py" code-line-numbers="6-8"}
import matplotlib.pyplot as plot
array_1x7 = np.array([1, 2, 2, 4, 1, 1, 7])
plot.plot(array_1x7)
plot.title("1x7 Array Visualization")
plot.xlabel("Index")
plot.ylabel("Value")
Expand All @@ -62,6 +81,28 @@ plot.show()
array_2x7 = np.array([[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 13]])
plot.imshow(array_2x7)
plot.show()
```

## Plotting 2D data

![](/media/2d-heatmap-pyplot-basic.png){.centre-full-image .top-55}


## Plotting 2D data

* Heatmaps are useful

```{.python filename="pyplot_2d.py" code-line-numbers="4-9"}
array_2x7 = np.array([[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 13]])
plot.imshow(array_2x7, cmap='viridis', aspect='auto')
plot.colorbar()
plot.title("2x7 Array Heatmap")
Expand All @@ -71,16 +112,64 @@ plot.ylabel("Rows")
plot.show()
```


## Plotting 2D data

![](/media/2d-heatmap-pyplot.png){.centre-full-image .top-55}


## Plotting 3D data

* Here is some 3-dimensional data to plot.

```{.python filename="pyplot_3d.py"}
array_3D = np.array([[[0, 80, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 111, 12, 13]],
[[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 122, 13, 14]],
[[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28]]])
```


## Plotting 3D data {.smaller}

* 3D scatter plots are useful

```{.python filename="pyplot_3d.py"}
```{.python filename="pyplot_3d.py" code-line-numbers="1-7,20"}
fig = plot.figure()
ax = fig.add_subplot(111, projection='3d')
x, y, z = np.indices(array_3D.shape)
values = array_3D.flatten()
sc = ax.scatter(x.flatten(), y.flatten(), z.flatten(), c=values, cmap='viridis', s=100)
plot.show()
```


## Plotting 3D data

![](/media/3d-scatterplot-pyplot-basic.png){.centre-full-image .top-55}


## Plotting 3D data {.smaller}

* 3D scatter plots are useful

```{.python filename="pyplot_3d.py" code-line-numbers="8-19"}
fig = plot.figure()
ax = fig.add_subplot(111, projection='3d')
Expand Down Expand Up @@ -108,7 +197,6 @@ plot.show()

![](/media/3d-scatterplot-pyplot.png){.centre-full-image .top-55}


## Now try it yourself!

* Go to the Lesson 3 folder.
Expand Down
146 changes: 143 additions & 3 deletions learn/learn-python/module-3/5-manipulating-images.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,159 @@ format: revealjs
logo: /media/ldd-logo.png
css: /slides.css
title-slide-attributes:
data-background-image: /media/jupyter-notebook-logo.png
data-background-opacity: "0.1"
data-background-image: /media/women-at-beach-sunset.jpg
data-background-opacity: "0.5"
---


## Python image library

* Image


## Introduction to Image Manipulation

* Image manipulation involves altering or transforming images using various techniques and tools.
* This can include resizing, cropping, rotating, adjusting colours, and applying filters.


## Loading Images

* To manipulate images, we first need to load them into our environment.
* In Python, we can use the `PIL` (also called Pillow) module from `Image`.


```{.python filename="images.py" code-line-numbers="1-2"}
from PIL import Image
import matplotlib.pyplot as plt
img_1 = Image.open('hand-x-ray.jpg')
plt.imshow(img_1)
plt.show()
```

## Loading Images

* You will also need to use the pyplot module.


```{.python filename="images.py" code-line-numbers="3"}
from PIL import Image
import matplotlib.pyplot as plt
img_1 = Image.open('hand-x-ray.jpg')
plt.imshow(img_1)
plt.show()
```


## Loading Images

* And then open the image file.


```{.python filename="images.py" code-line-numbers="5"}
from PIL import Image
import matplotlib.pyplot as plt
img_1 = Image.open('hand-x-ray.jpg')
plt.imshow(img_1)
plt.show()
```

## Loading Images {background-image="/media/two-spotlights.jpg" background-opacity="0.4"}

* And then prepare to show the image and then actually show it.

```{.python filename="images.py" code-line-numbers="7-9"}
from PIL import Image
import matplotlib.pyplot as plt
img_1 = Image.open('hand-x-ray.jpg')
plt.imshow(img_1)
plt.show()
```


## Resizing Images {background-image="/media/two-tape-measures.jpg" background-opacity="0.4"}

* To resize an image, you can use the `resize` method from the `PIL` library.
* `img_1.resize((width, height))` needs a tuple with the new width and height.

```{.python filename="resize.py"}
resized_img_1 = img_1.resize((50, 300))
plt.imshow(resized_img_1)
plt.show()
```


## Cropping Images {background-image="/media/yin-yang-hands.jpg" background-opacity="0.4"}


* Define the top-left and bottom-right coordinates of the area you want to crop.
* Then use the `crop` method from the `PIL` library.

```{.python filename="crop.py"}
box = (3900, 2900, 6000, 5500)
cropped_img_1 = img_1.crop(box)
```


## Rotate Images {background-image="/media/spirit-level.jpg" background-opacity="0.4"}

* Use the `rotate` method from the `PIL` library to rotate an image.
* Just provide an angle in degrees.

```{.python filename="rotate.py"}
rotated_img_2 = img_2.rotate(10)
```


## Inverting Images {background-image="/media/inverted-landscape-glass-sphere.jpg" background-opacity="0.4"}


* Invert with the PIL library using the `invert` method.

```{.python filename="rotate.py"}
from PIL import ImageOps
inverted_img_3 = ImageOps.invert(img_3)
```


## Change file type {background-image="/media/file-cabinets-old.jpg" background-opacity="0.4"}

* Use the `save` method from the `PIL` library to save the image in a different format.

```{.python filename="rotate.py"}
bmp_filename = 'converted_image_3.bmp'
inverted_img_3.save(bmp_filename)
```


## Now try it yourself!

* Go to the Lesson 4 folder.
* Open `lesson_4.ipynb`.
* Don't forget to ask your tutor if you need help.
* See you in 40 minutes.

If you finish lesson 4, you can try your hand at lesson 5.
If you finish lesson 4 (but no need to rush), you can try your hand at lesson 5.

```{=html}
<div class="bottom-right">
Expand Down
Binary file added media/1d-plot-pyplot-basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/2d-heatmap-pyplot-basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/3d-scatterplot-pyplot-basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/file-cabinets-old.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/inverted-landscape-glass-sphere.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/spirit-level.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/two-spotlights.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/two-tape-measures.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/women-at-beach-sunset.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/yin-yang-hands.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6a36eee

Please sign in to comment.