how to display graph legend with colored digraph

52 views
Skip to first unread message

Ben

unread,
Jul 26, 2016, 3:48:40 PM7/26/16
to sage-support
I'm trying to produce a digraph where some of the vertices are colored differently based on their properties. I have no trouble creating the partition or the vertex_color dictionary that can be passed either to .graphplot() or .show() to do this. However, I'm having trouble displaying some kind a legend to go along with the graph to explain which colors are which properties.

Could someone proficient in the graphing methods give me a hand here.

Here is a simple example, where I've colored some of the verticies.

DiGraph({0:[1], 1:[0], 2:[2],3:[3],4:[4]}).graphplot(vertex_colors={(0.0, 1.0, 1.0): [0, 1], (1.0, 0.0, 0.0): [2,3,4]})

I'd like to display with the graph something like

color1 = property A
color2 = property B

Thanks,
  Ben

Jori Mäntysalo

unread,
Jul 27, 2016, 2:43:17 AM7/27/16
to sage-support
On Tue, 26 Jul 2016, Ben wrote:

> .graphplot() or .show() to do this. However, I'm having trouble displaying
> some kind a legend to go along with the graph to explain which colors are
> which properties.

> I'd like to display with the graph something like
>
> color1 = property A
> color2 = property B

Good question. You can say

G = DiGraph({1:[2,3], 2:[3]})
G.show(vertex_colors={'red': G.sinks(), 'blue': G.sources()}, title="Sinks in red.\nSources in blue.")

but I don't know how to add text to a plot. For example

G = DiGraph({1:[2,3], 2:[3]})
gr = G.plot(vertex_colors={'red': G.sinks(), 'blue': G.sources()})
graphics_array([gr, text("Some colors\nto show special vertices.", (0,0))])

does something that you don't want. I guess that LaTeX (with, say, Tikz)
is needed for production quality pictures. Sage's graphics works for
demonstration in classroom.

--
Jori Mäntysalo

Ben

unread,
Jul 27, 2016, 11:30:50 AM7/27/16
to sage-support
That is great. Now, is there someway to permanently associate those two parameters to the graph? i.e., G.show() has the colors and title?

Jori Mäntysalo

unread,
Jul 27, 2016, 2:09:30 PM7/27/16
to sage-s...@googlegroups.com
Quoting Ben <bn4...@gmail.com>:

> That is great. Now, is there someway to permanently associate those two
> parameters to the graph? i.e., G.show() has the colors and title?

AFAIK no.

You can set positions for vertices with set_pos() and you can attach
an arbitrary value to a vertex like

G.set_vertex(2, 'red')

but there seems to be no way to otherwise set default options. There is

G.graphics_array_defaults

that says among other things "'graph_border': True", but it seems to
have no effect. Hopefully someone can tell what for it is.

* * *

New version of Sage will make it possible to say

G.show(vertex_color='red', vertex_colors={'blue': [0, 1], 'green': [2, 3, 4]})

so that you can have a default color for vertices not listed in
vertex_colors. Same will be available for edge colors.

--
Jori Mäntysalo

Reply all
Reply to author
Forward
0 new messages