Created
June 1, 2024 10:52
-
-
Save cvanelteren/2d8a07e610f8b48c944daf36248441a5 to your computer and use it in GitHub Desktop.
replace networkx nodes with images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
cvanelteren
commented
Jun 1, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment