Colour bar or legend...

2,724 views
Skip to first unread message

nickschurch

unread,
Feb 19, 2010, 10:39:26 AM2/19/10
to networkx-discuss
Hi all,

I'm really new to networkX but I'be been playing with it for a bit and
getting some good results. I have a network that I plot with colours
based on the pvalue associated with an expression value - is there any
way to add a colourbar, or a legend to the plot to show the range pf
pvalues displayed?

Nick

Dan Schult

unread,
Feb 19, 2010, 10:51:20 AM2/19/10
to networkx...@googlegroups.com
That would be a function in matplotlib (assuming you are using the
matplotlib drawing features of NetworkX).

Check out:
matplotlib.pyplot.colorbar()

http://matplotlib.sourceforge.net/api/
pyplot_api.html#matplotlib.pyplot.colorbar

That page has a lot of other nice drawing capabilities for matplotlib
that you will enjoy exploring. :) Since NetworkX uses matplotlib
any of them should work with your project.
Dan

> --
> You received this message because you are subscribed to the Google
> Groups "networkx-discuss" group.
> To post to this group, send email to networkx-
> dis...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discuss
> +unsub...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/
> group/networkx-discuss?hl=en.
>

nickschurch

unread,
Feb 22, 2010, 7:20:07 AM2/22/10
to networkx-discuss
Thanks for the response! Colorbar seems to work, in that it does
indeed draw a colour bar, but it draws it for the edge colour values,
not for the node colour values. I can't see anything in the colorbar
api or documentation that allows you do choose which values of a graph
it applies to or allows you to foce a range for it. Any ideas?

nickschurch

unread,
Feb 22, 2010, 7:23:39 AM2/22/10
to networkx-discuss
Interestingly, if you don't specify edge colours then it will plot the
colorbar for the node values... I wonder if its because networkX plots
the edges after the nodes....

Aric Hagberg

unread,
Feb 22, 2010, 9:32:56 AM2/22/10
to networkx...@googlegroups.com
On Mon, Feb 22, 2010 at 5:23 AM, nickschurch <nicks...@googlemail.com> wrote:
> Interestingly, if you don't specify edge colours then it will plot the
> colorbar for the node values... I wonder if its because networkX plots
> the edges after the nodes....

That is the reason. With a little more work you can control which
colorbar (you can even have 2). e.g.


import matplotlib.pyplot as plt
import networkx as nx

G=nx.star_graph(20)
pos=nx.spring_layout(G)
node_colors=range(21)
edge_colors=range(20)
nodes=nx.draw_networkx_nodes(G,pos,node_color=node_colors,cmap=plt.cm.Reds)
edges=nx.draw_networkx_edges(G,pos,edge_color=edge_colors,width=4,edge_cmap=plt.cm.Blues)
plt.sci(nodes)
plt.colorbar()
plt.sci(edges)
plt.colorbar()
plt.show() # display

Aric

nickschurch

unread,
Feb 22, 2010, 1:26:43 PM2/22/10
to networkx-discuss
Thanks Aric - I wasn't aware I could plot each part separately putting
a colourbar with each! Nice!

On 22 Feb, 14:32, Aric Hagberg <ahagb...@gmail.com> wrote:

Uli

unread,
Sep 27, 2017, 8:32:28 AM9/27/17
to networkx-discuss
Hi,
I try to get a legend with '^' marker, colors and textlabels to my graph.
I tried many ways, in the end getting a proper legend with markers and labels but without the proper colors.
It always seems to show the max. colorindex of the colormap. 
Using this example I succeeded in putting a colorbar to my graph with the proper colors but labeled by colorindex and not by label.
so how to get a legend to it. 
The result is a legend with proper labels, lines instead of '^' and wrong colors.
colors is a [1,2,2,5,3,...]
edgecolors is a [1,1,1,1,1...]
cmapC = matplotlib.cm.get_cmap('Spectral')

nc = nx.draw_networkx_nodes(GM,graph_pos,node_size=200, node_text_size=6, node_shape='^',
   alpha=1.0, node_color=colors, cmap=cmapC )   #      , label = "Orte"
ne = nx.draw_networkx_edges(GM,graph_pos,width=0.3,
   alpha=1.0,edge_color=edgecolors, edge_cmap=plt.cm.Blues, arrows=False)
nl = nx.draw_networkx_labels(GM, graph_pos, labels=Place_labels, font_size=6)

plt.axis('on')

plt.sci(nc)
plt.colorbar()
plt.legend(scatterpoints=1)
plt.sci(ne)
plt.colorbar()
plt.legend()
plt.show()
Reply all
Reply to author
Forward
0 new messages