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





Show thread

@s_gruppetta Oh nice, I didn't know about subplot_mosaic(). In many ways similar to the proplot "array" subplots specification, which also comes with a bunch of other nice science-focused goodies for extending matplotlib proplot.readthedocs.io/en/stab

@jford I’ve not used proplot before, may have a look at it at some point!

@s_gruppetta I find it very nice, it's replaced a lot of the crufty matplotlib boilerplate I've accumulated over the years for creating publication-ready figures. You can always drop back down to "raw" matplotlib if you need to do something very specific.

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.