Have you used subplot_mosaic() in ’s 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!


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

Show thread

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", "."]]


# 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





Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.