Graph.show() and non-injective relabeling

275 views
Skip to first unread message

Jori Mantysalo

unread,
Oct 2, 2014, 11:06:20 AM10/2/14
to sage-...@googlegroups.com
A ticket about this: http://trac.sagemath.org/ticket/15206 . This is not
related to posets only, but to graphs in general. Is anyone planning to do
this?

It seems that mostly part "append(text(str(v)" at set_vertices on
graph_plot.py must be changed, and the chain of function calls changed to
forward relabeling dict to there. But I am not sure if for example dot2tex
complicates this. (Because it, for example, reverses poset -- see #16865.)

--
Jori Mäntysalo

Nathann Cohen

unread,
Oct 3, 2014, 11:01:03 AM10/3/14
to sage-...@googlegroups.com
A ticket about this: http://trac.sagemath.org/ticket/15206 . This is not
related to posets only, but to graphs in general. Is anyone planning to do
this?

I do not understand: do you only want to draw a graph with "anything you want" as labels of the vertices ?

If so, I have no idea why it seems to have been half-implemented in the posets file. That's clearly a Graph matter, which Posets would then call. This being said, you should not make this work by relabelling the graph itself: if several vertices of a graph have the same name, we would have no way to do things as simple as getting the degree of a vertex.

This being said, there is in Graph.plot() an option currently named vertex_labels which can be set to True/False according to whether you want to display the vertices' labels when the graph is drawn. If you just make it accept a dictionary of labels, there must then be a way to plot the labels you gave this way instead of the vertices' names.

Nathann

Jori Mantysalo

unread,
Oct 14, 2014, 1:33:20 AM10/14/14
to sage-...@googlegroups.com
On Fri, 3 Oct 2014, Nathann Cohen wrote:

>> A ticket about this: http://trac.sagemath.org/ticket/15206 . This is
>> not related to posets only, but to graphs in general. Is anyone
>> planning to do this?

> I do not understand: do you only want to draw a graph with "anything you
> want" as labels of the vertices ?

Yes. Just like adges can be labelled so that more than one edge has same
label:

G=DiGraph({0:[1,2]})
G.set_edge_label(0,1,'Hi!')
G.set_edge_label(0,2,'Hi!')

(But ".relabel(lambda e: ...)" -syntax is easier, I think.)

> If so, I have no idea why it seems to have been half-implemented in the
> posets file. That's clearly a Graph matter, which Posets would then call.
> This being said, you should not make this work by relabelling the graph
> itself: if several vertices of a graph have the same name, we would have no
> way to do things as simple as getting the degree of a vertex.
>
> This being said, there is in Graph.plot() an option currently named
> vertex_labels which can be set to True/False according to whether you want
> to display the vertices' labels when the graph is drawn. If you just make it
> accept a dictionary of labels, there must then be a way to plot the labels
> you gave this way instead of the vertices' names.

Sounds like a great plan! Now we just wait for somebody to do it!

I will not, not at least in any near future.

* * *

Btw, why default color changes when you give color of one element? See

G=DiGraph({0:[1]})
G.show(vertex_colors='pink')
G.show(vertex_colors={'red':[0]})

changing background color of "1".

--
Jori Mäntysalo

Nathann Cohen

unread,
Oct 14, 2014, 5:45:00 AM10/14/14
to Sage devel
YooooooooooOO !

> Yes. Just like adges can be labelled so that more than one edge has same label:
>
> G=DiGraph({0:[1,2]})
> G.set_edge_label(0,1,'Hi!')
> G.set_edge_label(0,2,'Hi!')
>
> (But ".relabel(lambda e: ...)" -syntax is easier, I think.)

Yes but we can't do the same for the vertices of a graph, of we would
have no way to differentiate them afterwards. It should really be a
plot parameter.

> Sounds like a great plan! Now we just wait for somebody to do it!

Well, perhaps we will do it ourselves some day.

> Btw, why default color changes when you give color of one element? See

Probably to make sure that you did not give the default color to that
element. Well O_o

Nathann

Jori Mantysalo

unread,
Oct 14, 2014, 5:50:05 AM10/14/14
to Sage devel
On Tue, 14 Oct 2014, Nathann Cohen wrote:

>> G=DiGraph({0:[1,2]})
>> G.set_edge_label(0,1,'Hi!')
>> G.set_edge_label(0,2,'Hi!')
>>
>> (But ".relabel(lambda e: ...)" -syntax is easier, I think.)
>
> Yes but we can't do the same for the vertices of a graph, of we would
> have no way to differentiate them afterwards. It should really be a
> plot parameter.

That was what I was thinking. I don't know if "relabel" is good name for
parameter of plot(). Maybe not.

--
Jori Mäntysalo

Liang Ze Wong

unread,
Dec 9, 2014, 3:01:32 AM12/9/14
to sage-...@googlegroups.com
Here's a hackish solution I got by digging into the graph_plot sourcecode: http://git.sagemath.org/sage.git/tree/src/sage/graphs/graph_plot.py
The relevant lines are 424 - 426.

I created a GraphPlot object and modified the labels there instead of in the original graph.


G=DiGraph({0:[1,2]})


Gplot = G.graphplot()
# Extract relevant components
node_list
= Gplot._nodelist
pos_dict
= Gplot._pos


# Define list or dict of labels (same length as node_list)
label_list
= ["Ho","Hi","Hi"]

# Modify vertex labels
Gplot._plot_components['vertex_labels'] = [text(label, pos_dict[node], rgbcolor=(0,0,0), zorder=8) for node,label in zip(node_list,label_list)]
Gplot.show()

You could probably condense it into a two or three liner. 

It seems like it should also be possible to label vertices with non-text as well (e.g. other graphics components).

Regards,
Ze

Jori Mantysalo

unread,
Dec 9, 2014, 6:04:38 AM12/9/14
to sage-...@googlegroups.com
On Tue, 9 Dec 2014, Liang Ze Wong wrote:

> Here's a hackish solution I got by digging into the graph_plot
> sourcecode: http://git.sagemath.org/sage.git/tree/src/sage/graphs/graph_plot.pyThe relevant lines are 424 - 426.

+1 for remembering this. I hope that someone who knows graphcis can check
if this works correctly.

> It seems like it should also be possible to label vertices with non-text
> as well (e.g. other graphics components).

That would be nice. A simple example could be having LaTeX-parsed strings
as labels.

--
Jori Mäntysalo
Reply all
Reply to author
Forward
0 new messages