How to use an image as a vertex label in a graph?

asked 2019-10-29 16:47:43 +0100

Sultan gravatar image

Exactly what the title says. I have a graph, but I'd like to change the labels of the vertices so that I can display my own images at each vertex.

I figured something like the code below would work, but instead of showing the images at the vertices, I see a string of where the image is located in memory.

from PIL import Image

H = Graph({img0:[1], 1:[2], 2:[3]})

img0 = Image.open('image0.png')
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')
img3 = Image.open('image3.png')

img_list = [img0 ,img1, img2, img3]

def my_label_function (vertex_number):
    return img_list[vertex_number]

H.relabel(my_label_function)

H.plot()
edit retag flag offensive close merge delete

Comments

That's no surprise--vertex labels in graphs can only be strings or numeric values; Sage wouldn't know what to do with some arbitrary object like a PIL Image, thought it's a clever attempt. You probably wouldn't want, in any abstract sense, to use images as vertex labels anyways, outside the context of plotting. This is not so well-documented, but the vertices of simple graphs are plotted using scatter_plot, and you can pass vertex_shape to specify any of the "markers" supported by matplotlib, though it seems Sage currently (unhelpfully in this case) prevents passing a Path object for a custom marker shape. And using an arbitrary image may take additional hacking at the matplotlib level.

Iguananaut gravatar imageIguananaut ( 2019-11-05 15:22:43 +0100 )edit