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
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()
@s_gruppetta Oh, my. I think I'm in love. Thanks for sharing this!
@s_gruppetta interesting 🤔
@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 https://proplot.readthedocs.io/en/stable/basics.html#Creating-subplots
@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.
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