Have you used `subplot_mosaic()` in #Python's #Matplotlib yet?
Or are you a dinosaur like me and still use older functions? After all, `subplot_mosaic()` was only introduced in 2020 in version 3.3
Recently, I decided to finally explore `subplot_mosaic()` and I know I'll never go back to whatever I did before to plot these types of figures!
<code in next post>
/1
#coding #programming #Visualisation #LearnPython #LearnToCode
Here's the code for the plot in the previous post - put in your own image file, of course!
You can get the text from the images's ALT text
Well, the code was too long for the ALT text, so here's the code pasted here:
import matplotlib.pyplot as plt
image = plt.imread("gardens.jpeg")
# Create a figure using a 4x4 array.# The "." indicates an empty space in the gridfig, ax = plt.subplot_mosaic( [["Colour", "Colour", "Colour", "Hist Red"], ["Colour", "Colour", "Colour", "Hist Green"], ["Colour", "Colour", "Colour", "Hist Blue"], ["Red", "Green", "Blue", "."]])
ax["Colour"].imshow(image)ax["Colour"].axis("off")
# Plot histograms for red, green, and blue channelsax["Hist Red"].hist(image[:, :, 0].ravel(), bins=50, color="red")ax["Hist Red"].axis("off")ax["Hist Green"].hist(image[:, :, 1].ravel(), bins=50, color="green")ax["Hist Green"].axis("off")ax["Hist Blue"].hist(image[:, :, 2].ravel(), bins=50, color="blue")ax["Hist Blue"].axis("off")
# Extract red, green, and blue channels from imageimage_red = image.copy()image_red[:, :, 1:] = 0image_green = image.copy()image_green[:, :, 0] = 0image_green[:, :, 2] = 0image_blue = image.copy()image_blue[:, :, :2] = 0
ax["Red"].imshow(image_red)ax["Red"].axis("off")
ax["Green"].imshow(image_green)ax["Green"].axis("off")
ax["Blue"].imshow(image_blue)ax["Blue"].axis("off")
fig.show()
QOTO: Question Others to Teach Ourselves An inclusive, Academic Freedom, instance All cultures welcome. Hate speech and harassment strictly forbidden.
Well, the code was too long for the ALT text, so here's the code pasted here:
import matplotlib.pyplot as plt
image = plt.imread("gardens.jpeg")
# Create a figure using a 4x4 array.
# The "." indicates an empty space in the grid
fig, ax = plt.subplot_mosaic(
[["Colour", "Colour", "Colour", "Hist Red"],
["Colour", "Colour", "Colour", "Hist Green"],
["Colour", "Colour", "Colour", "Hist Blue"],
["Red", "Green", "Blue", "."]]
)
ax["Colour"].imshow(image)
ax["Colour"].axis("off")
# Plot histograms for red, green, and blue channels
ax["Hist Red"].hist(image[:, :, 0].ravel(), bins=50, color="red")
ax["Hist Red"].axis("off")
ax["Hist Green"].hist(image[:, :, 1].ravel(), bins=50, color="green")
ax["Hist Green"].axis("off")
ax["Hist Blue"].hist(image[:, :, 2].ravel(), bins=50, color="blue")
ax["Hist Blue"].axis("off")
# Extract red, green, and blue channels from image
image_red = image.copy()
image_red[:, :, 1:] = 0
image_green = image.copy()
image_green[:, :, 0] = 0
image_green[:, :, 2] = 0
image_blue = image.copy()
image_blue[:, :, :2] = 0
ax["Red"].imshow(image_red)
ax["Red"].axis("off")
ax["Green"].imshow(image_green)
ax["Green"].axis("off")
ax["Blue"].imshow(image_blue)
ax["Blue"].axis("off")
fig.show()