diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2021-06-16 19:38:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-16 12:38:12 -0400 |
commit | 1f96b154a86588a7ef3d9b3637dd66749b1de71f (patch) | |
tree | 9670e2fb9d74c632017308caafa14dcae4954c8f | |
parent | 168bcb82e0872b3cd6400efc9f1f428498adffeb (diff) | |
download | networkx-1f96b154a86588a7ef3d9b3637dd66749b1de71f.tar.gz |
Doc/fix 403 error drawing custom icons (#4906)
* Add local copies of custom node icon pngs.
* Update example to use local images.
* Force FancyArrowPatch to respect tgt distances.
min/max_target_distance kwargs are only supported by FancyArrowPatch.
Force drawing with FAPs and add comment explaining how/why.
* Add seed to layout for reproducibility.
-rw-r--r-- | examples/drawing/icons/computer_black_144x144.png | bin | 0 -> 485 bytes | |||
-rw-r--r-- | examples/drawing/icons/router_black_144x144.png | bin | 0 -> 937 bytes | |||
-rw-r--r-- | examples/drawing/icons/switch_black_144x144.png | bin | 0 -> 546 bytes | |||
-rw-r--r-- | examples/drawing/plot_custom_node_icons.py | 35 |
4 files changed, 23 insertions, 12 deletions
diff --git a/examples/drawing/icons/computer_black_144x144.png b/examples/drawing/icons/computer_black_144x144.png Binary files differnew file mode 100644 index 00000000..bddc3a07 --- /dev/null +++ b/examples/drawing/icons/computer_black_144x144.png diff --git a/examples/drawing/icons/router_black_144x144.png b/examples/drawing/icons/router_black_144x144.png Binary files differnew file mode 100644 index 00000000..51588d30 --- /dev/null +++ b/examples/drawing/icons/router_black_144x144.png diff --git a/examples/drawing/icons/switch_black_144x144.png b/examples/drawing/icons/switch_black_144x144.png Binary files differnew file mode 100644 index 00000000..edf38c58 --- /dev/null +++ b/examples/drawing/icons/switch_black_144x144.png diff --git a/examples/drawing/plot_custom_node_icons.py b/examples/drawing/plot_custom_node_icons.py index dd88413b..c13458f8 100644 --- a/examples/drawing/plot_custom_node_icons.py +++ b/examples/drawing/plot_custom_node_icons.py @@ -4,24 +4,23 @@ Custom node icons ================= Example of using custom icons to represent nodes with matplotlib. + +Images for node icons courtesy of www.materialui.co """ import matplotlib.pyplot as plt import networkx as nx import PIL -import urllib.request # Image URLs for graph nodes -icon_urls = { - "router": "https://www.materialui.co/materialIcons/hardware/router_black_144x144.png", - "switch": "https://www.materialui.co/materialIcons/action/dns_black_144x144.png", - "PC": "https://www.materialui.co/materialIcons/hardware/computer_black_144x144.png", +icons = { + "router": "icons/router_black_144x144.png", + "switch": "icons/switch_black_144x144.png", + "PC": "icons/computer_black_144x144.png", } -# Load images from web -images = { - k: PIL.Image.open(urllib.request.urlopen(url)) for k, url in icon_urls.items() -} +# Load images +images = {k: PIL.Image.open(fname) for k, fname in icons.items()} # Generate the computer network graph G = nx.Graph() @@ -39,10 +38,22 @@ for u in range(1, 4): for v in range(1, 4): G.add_edge("switch_" + str(u), "PC_" + str(u) + "_" + str(v)) -# get layout and draw edges -pos = nx.spring_layout(G) +# Get a reproducible layout and create figure +pos = nx.spring_layout(G, seed=1734289230) fig, ax = plt.subplots() -nx.draw_networkx_edges(G, pos=pos, ax=ax, min_source_margin=15, min_target_margin=15) + +# Note: the min_source/target_margin kwargs only work with FancyArrowPatch objects. +# Force the use of FancyArrowPatch for edge drawing by setting `arrows=True`, +# but suppress arrowheads with `arrowstyle="-"` +nx.draw_networkx_edges( + G, + pos=pos, + ax=ax, + arrows=True, + arrowstyle="-", + min_source_margin=15, + min_target_margin=15, +) # Transform from data coordinates (scaled between xlim and ylim) to display coordinates tr_figure = ax.transData.transform |