2

I have a python notebook and using graphviz for making a graph I get all characters showing correctly on VS Code, but on github all latin and chinese characters show incorrectly like this:

enter image description here

I know the problem is on the encoding, but it's related to the graphviz library. I don't have problems showing special characters on strings or other graphs.

This is my code:

from graphviz import Digraph
h = Digraph("Shu")
h.attr(rankdir="LR",fontname="verdana")

with h.subgraph(name='cluster_wu') as c:
    c.attr(label='5 Shu antigos 五輸穴', style='rounded,filled', color='blanchedalmond')
    c.node_attr.update(style='filled', color='white')
    c.node('a1','Poço\n井穴', color='white')
    c.node('a2',"Manancial\n滎穴", color='lightgray')
    c.node('a3',"Riacho\n輸穴", color='darkgray')
    c.node('a4',"Rio\n經穴", color='gray40')
    c.node('a5',"Mar\n合穴", color='gray20', fontcolor='white')
    c.edges([('a1', 'a2'), ('a2', 'a3'), ('a3', 'a4'), ('a4', 'a5')])
h

I tried declaring UTF8 with no luck:

# -*- coding: utf-8 -*-

Thanks for any help I can get.

andrewJames
  • 19,570
  • 8
  • 19
  • 51

1 Answers1

1

by default this library uses as fontname: Times-roman, this is caused because probably the fontname is not compatible with chinese characters, I can relate this to another similar question and I think that the most recommended thing is to change the fontname to a compatible font for your task:

command line program exec:

-Nfontname="YRDZST"

or

digraph {
    label="YRDZST"
    fontname="YRDZST"
    ...
}

You can also use a external file, for this task you shall follow a series of steps that are indicated from documentation

user11717481
  • 1
  • 9
  • 15
  • 25
  • Thank you for your help. I defined the font "verdana" that is able to show all chars I need. I tried the "hanyi" and it didn't work either. I know now that it's encoding the characters in UTF-8, don't know why isn't showing correctly though. – Rodrigo Sze Nov 21 '22 at 17:15