getting my bearings: help with drawing colors

27 views
Skip to first unread message

davetuu

unread,
Nov 26, 2009, 11:09:57 PM11/26/09
to networkx-discuss
I'm experimenting with NetworkX as a graph library for use in mapping
tcp/ip networks for a few different practical applications. I found
my way here through looking for more drawing/plotting flexibility than
from GraphViz, but I'm struggling to learn how to control the output.

The first application is a digraph to map associations in an NTP
(Network Time Protocol) subnet; I'm starting to wonder whether
matplotlib is really the tool for this or whether I should concentrate
on graphviz. What I need to learn how to make matplotlib do is:

1: Set the node/vertex shape and/or color based on the NTP stratum
level (an integer from 0 -- 16).
2: Control the placement/formatting of the node label text.
3: Influence the plot shape such that the lower stratum nodes are
drawn "near" each other, preferably near the "top" of the drawing
area.
4: Use edge attributes to control the edge appearance (color,
thickness, line style)

At the moment, my nodes are just strings (ipv4 dotquads) as I haven't
figured out how to get the nodes back from the graph if the keys are
other than simple strings. So I have a separate dict to hold the node
attributes. This means that the graph node itself doesn't have the
attribute I want to use to control the color/shape.

I can't figure out if a colormap is useful in this situation; what is
used as the key for a colormap? Or are they not applicable to scatter
plots? (I realize this is more of a matplotlib question, but I need
to understand how networkx is using matplotlib to be able to ask over
there)

I just need to put together a few concepts before I have enough
context to find my way through the docs.


Thanks.

Aric Hagberg

unread,
Nov 27, 2009, 8:46:59 AM11/27/09
to networkx...@googlegroups.com
On Thu, Nov 26, 2009 at 9:09 PM, davetuu <dav...@gmail.com> wrote:
> on graphviz.  What I need to learn how to make matplotlib do is:
>
> 1: Set the node/vertex shape and/or color based on the NTP stratum
> level (an integer from 0 -- 16).
> 2: Control the placement/formatting of the node label text.
> 3: Influence the plot shape such that the lower stratum nodes are
> drawn "near" each other, preferably near the "top" of the drawing
> area.
> 4: Use edge attributes to control the edge appearance (color,
> thickness, line style)
>
> At the moment, my nodes are just strings (ipv4 dotquads) as I haven't
> figured out how to get the nodes back from the graph if the keys are
> other than simple strings.  So I have a separate dict to hold the node
> attributes.  This means that the graph node itself doesn't have the
> attribute I want to use to control the color/shape.

There are two parts - the layout of the nodes and the drawing. NetworkX
has a few layout algorithms (and an interface to Graphviz to use those
layout algorithms) and uses Matplotlib for drawing. NetworkX was
not orignally designed as a graph drawing package so it doesn't use
very sophisticated techniques to make the drawings beautiful (or easy to make).

The interface to Matplotlib is clunky but in principle you can do everything
in that Matplotlib can do. The interface is based on Matplotlib
scatter so you can
look at the Matplotlib scatter docs to see more details.

1) In practice the interface is somewhat limited since we dont allow all of the
scatter keywords (marker= is one of those). The NetworkX interface could be
pretty easily hacked to allow that.

2) The text drawing is crude. We try to place the text at the node position...

3) I'm not sure which node positioning algorithm would be best for that.
Maybe if you assign the stratum to "edge weight" one of the graphviz layouts
(dot?) would do something like that.

4) Those are properties for matplotlib "Line Collections" and the
names are the same.

Take a look at the NetworkX examlples for some hints:
http://networkx.lanl.gov/gallery.html

Attributes can be assigned to nodes and edges. They are just put in
dictionaries,
e.g. The

In [1]: import networkx as nx

In [2]: G=nx.DiGraph()

In [3]: G.add_node(1,foo='bar')

In [4]: G.node[1]
Out[4]: {'foo': 'bar'}

In [5]: G.node[1]['foo']
Out[5]: 'bar'

In [6]: G.add_edge(1,2,bandwidth=7)

In [7]: G[1][2]
Out[7]: {'bandwidth': 7}

In [8]: G[1][2]['bandwidth']
Out[8]: 7


> I can't figure out if a colormap is useful in this situation; what is
> used as the key for a colormap?  Or are they not applicable to scatter
> plots?  (I realize this is more of a matplotlib question, but I need
> to understand how networkx is using matplotlib to be able to ask over
> there)

You have to specify the values explictly using the "node_colors" or
"edge_color" keywords
to networkx.draw* functions.

Aric
Reply all
Reply to author
Forward
0 new messages