Skip to content

Instantly share code, notes, and snippets.

@cvanelteren
Created June 1, 2024 10:52
Show Gist options
  • Save cvanelteren/2d8a07e610f8b48c944daf36248441a5 to your computer and use it in GitHub Desktop.
Save cvanelteren/2d8a07e610f8b48c944daf36248441a5 to your computer and use it in GitHub Desktop.
replace networkx nodes with images
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
g = nx.krackhardt_kite_graph()
pos = nx.kamada_kawai_layout(g)
example = "unknown.jpg"
fig, ax = plt.subplots()
output_size = 25
nx.draw_networkx_edges(g, pos, ax = ax)
for node, loc in pos.items():
img = plt.imread(example)
height, width, _ = img.shape
aspect_ratio = width / height
zoom = output_size / max(height, width)
box = OffsetImage(img, zoom=zoom)
ab = AnnotationBbox(box,
loc,
xybox = (0,0),
frameon=False,
boxcoords = "offset points",
)
ax.add_artist(ab)
ax.axis("equal")
ax.grid(False)
fig.show()
@cvanelteren
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment