Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#21008 closed defect (fixed)

Allow non-ascii labels in graph vertices (at least for plotting)

Reported by: kcrisman Owned by:
Priority: minor Milestone: sage-7.3
Component: graph theory Keywords:
Cc: Merged in:
Authors: Frédéric Chapoton Reviewers: Jori Mäntysalo
Report Upstream: N/A Work issues:
Branch: e6523a1 (Commits) Commit:
Dependencies: Stopgaps:

Description

Apparently graph vertex labels have to be ASCII.

Graph({'Б':[1,2]}).plot()

See here and the duplicate here.

Change History (18)

comment:1 Changed 4 years ago by chapoton

  • Authors set to Frédéric Chapoton
  • Branch set to u/chapoton/21008
  • Commit set to 6b6633c2dca7b0e1b8bd9917c95013e9031cf931
  • Status changed from new to needs_review

Here is a tentative. Sometimes the character is not available in the usual font. At least this works for the more common utf8 characters.

http://stackoverflow.com/questions/10960463/non-ascii-characters-in-matplotlib


New commits:

6b6633ctrac 21008 allow unicode labels in graphs

comment:2 Changed 4 years ago by jmantysalo

  • Reviewers set to Jori Mäntysalo

Mäntysalo can review this patch made by Frédéric.

comment:3 Changed 4 years ago by kcrisman

I take it that would create some self-referential directed graph of review with Unicode vertices :-) just make sure it doesn't break any current graph plots!

comment:4 Changed 4 years ago by paulmasson

Documentation doesn't build with this fix. See #13161 for similar problem.

Also, the Russian character displays as a square box on Mac OS X El Capitan. Local Unicode fonts do not get incorporated into matplotlib output. Shouldn't there be some simple way to use local fonts?

comment:5 Changed 4 years ago by git

  • Commit changed from 6b6633c2dca7b0e1b8bd9917c95013e9031cf931 to 7d03de3d417c4f450eb2c05b460c702d97f4f7e8

Branch pushed to git repo; I updated commit sha1. New commits:

7d03de3trac 21008 utf8 encoding of file, plus nicer doctest

comment:6 follow-up: Changed 4 years ago by jmantysalo

I don't understand. This works:

DiGraph({u'Mäntysalo':[u'Frédéric']}).plot()

but this does not:

D = DiGraph()
D.add_edge(u'Mäntysalo', u'Frédéric')
G.show()

comment:7 Changed 4 years ago by jmantysalo

  • Status changed from needs_review to needs_work

Also src/sage/combinat/posets/posets.py fails some test.

comment:8 in reply to: ↑ 6 Changed 4 years ago by chapoton

Replying to jmantysalo:

I don't understand. This works:

DiGraph({u'Mäntysalo':[u'Frédéric']}).plot()

but this does not:

D = DiGraph()
D.add_edge(u'Mäntysalo', u'Frédéric')
G.show()

It should be D.show(), and it works for me.

comment:9 Changed 4 years ago by git

  • Commit changed from 7d03de3d417c4f450eb2c05b460c702d97f4f7e8 to e6523a1bddb868d47353f1f5234f24f183d74865

Branch pushed to git repo; I updated commit sha1. New commits:

e6523a1trac 21008 fixing doctests in posets

comment:10 Changed 4 years ago by chapoton

  • Status changed from needs_work to needs_review

comment:11 Changed 4 years ago by jmantysalo

  • Status changed from needs_review to positive_review

My mistake. Now it works and also

D = DiGraph()
D.add_edge(u'Frédéric', 'review', u'Mäntysalo')
D.show(edge_labels=True)

works.

comment:12 Changed 4 years ago by kcrisman

Do we want all vertex labels to be unicode automatically? (I'm not sure if that is what is happening with the changed doctest in posets.)

comment:13 Changed 4 years ago by chapoton

labels are converted to unicode in the plot, not in the graphs themselves.

comment:14 Changed 4 years ago by kcrisman

Got it, thanks.

comment:15 Changed 4 years ago by vbraun

  • Branch changed from u/chapoton/21008 to e6523a1bddb868d47353f1f5234f24f183d74865
  • Resolution set to fixed
  • Status changed from positive_review to closed

comment:16 Changed 4 years ago by tmonteil

  • Commit e6523a1bddb868d47353f1f5234f24f183d74865 deleted

On my machine (7.4.beta1), the example given in the ticket description does not work:

sage: Graph({'Б':[1,2]}).plot()
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
Version 0, edited 4 years ago by tmonteil (next)

comment:17 Changed 4 years ago by chapoton

you forgot the u. Ths is not an ascii string. As long as nobody cares about py3, we have to add a u for unicode strings.

comment:18 Changed 4 years ago by tmonteil

The point was that 'Б' is a valid string (through not unicode), which is not handled by plot. If it will automatically be fixed by Python 3, it is probably better not to add artificial noise into the source code.

Note: See TracTickets for help on using tickets.