Custom Node Images

瀏覽次數:3,059 次
跳到第一則未讀訊息

Steve

未讀,
2011年5月10日 清晨7:34:092011/5/10
收件者:networkx-discuss
I'm looking to do a fairly straightforward task - produce simple
application architecture diagrams. The architecture would vary in
complexity, but an example would be a load balancer pointing to two
web servers. The two web servers pointing to another load balancer.
The load balancer directing traffic to two nodes representing a
database cluster.

I'd like to be able to customize the images - have one image for web
servers, another for load balancers, another for database servers.

I build these simple diagrams in visio all the time to show people a
basic overview of how their environments will look. I had the idea
last night of automating it so I don't have to copy-paste and build
new diagrams every time.

I played with pydot/graphviz a little bit last night and was able to
get the diagrams right with basic shapes, but when I tried to put in
images, I started to run into problems. Researching them, I found
networkx, and it seems like it might be able to do it a little bit
easier.

Quoting from the tutorial: Nodes can be any hashable object e.g. a
text string, an image, an XML object, another Graph, a customized node
object, etc.

If my nodes are images, when I draw my graph, will the images be used
to display the nodes? Or do I need to set an attribute? I've looked
through the examples, and the ones I see, all the nodes seem to be
circles.

I do realize that networkx isn't the drawing engine, but I was hoping
this was something people did all the time and the answer was simple.

Aric Hagberg

未讀,
2011年5月10日 上午9:33:322011/5/10
收件者:networkx...@googlegroups.com

There is no built-in way but with a little hacking you can do it.
Here is an example that I made a while back to show pie charts as
nodes but modified to use images.
The scaling transforms aren't quite right -maybe a matplotlib hacker
can help us there.
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# image from http://matplotlib.sourceforge.net/users/image_tutorial.html
img=mpimg.imread('stinkbug.png')
G=nx.complete_graph(4)

G.node[0]['image']=img
G.node[1]['image']=img
G.node[2]['image']=img
G.node[3]['image']=img

pos=nx.spring_layout(G)

fig=plt.figure(figsize=(5,5))
ax=plt.subplot(111)
ax.set_aspect('equal')
nx.draw_networkx_edges(G,pos,ax=ax)

plt.xlim(-0.5,1.5)
plt.ylim(-0.5,1.5)

trans=ax.transData.transform
trans2=fig.transFigure.inverted().transform

piesize=0.2 # this is the image size
p2=piesize/2.0
for n in G:
xx,yy=trans(pos[n]) # figure coordinates
xa,ya=trans2((xx,yy)) # axes coordinates
a = plt.axes([xa-p2,ya-p2, piesize, piesize])
a.set_aspect('equal')
a.imshow(G.node[n]['image'])
a.axis('off')

plt.show()

回覆所有人
回覆作者
轉寄
0 則新訊息