Toool
Toool

Reputation: 361

Matplotlib: Uneven grid of images on y axis scale

I would like to plot an array of images (molecular orbitals) on a y scale unevenly, according to their y value (energy), with matplotlib.

I know a grid of images can be done with subplots, but this is limited to evenly spaced images, which is not what I want. Moreover, I want a single y scale to represent the values associated to each image.

Upvotes: 0

Views: 274

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

You can use an OffsetImage placed inside an AnnotationBbox to position your image anywhere you like.

import matplotlib.pyplot as plt
from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)

def image(x, y, ax, fname, zoom=0.5):
    """Place image from file `fname` into axes `ax` at position `x,y`."""
    ar = plt.imread(fname)
    im = OffsetImage(ar, zoom=zoom)
    im.image.axes = ax

    ab = AnnotationBbox(im, (x,y), xycoords='data')
    ax.add_artist(ab)


path = "https://upload.wikimedia.org/wikipedia/commons/thumb/"
exts = ["1/15/Hydrogen_eigenstate_n1_l0_m0_wedgecut.png/30px-Hydrogen_eigenstate_n1_l0_m0_wedgecut.png",
        "c/c6/Hydrogen_eigenstate_n2_l1_m0.png/50px-Hydrogen_eigenstate_n2_l1_m0.png",
        "2/21/Hydrogen_eigenstate_n3_l1_m0.png/70px-Hydrogen_eigenstate_n3_l1_m0.png",
        "f/f5/Hydrogen_eigenstate_n3_l1_m-1.png/70px-Hydrogen_eigenstate_n3_l1_m-1.png",
        "6/6a/Hydrogen_eigenstate_n3_l2_m1.png/70px-Hydrogen_eigenstate_n3_l2_m1.png"]

fig, ax = plt.subplots()
ax.axis([-1,5,-1,5])
for i, p in enumerate(exts):
    image(i, i, ax, path+p)

plt.show()

enter image description here

Upvotes: 2

Related Questions