@th8687rr

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Graphviz 日本語フォントの文字化け

Graphviz 日本語フォントの文字化け

Google Colaboratory上で、Pythonを使ったGraphvizを作成しています。
例えば下記のコードでpdfにてrenderingすると、pdfに「犬」「猫」の表記が文字化けした状態で保存されます。
Google Colaboratory画面上では問題なく日本語表記されます。
renderingに何か問題があるのか、、分からず、どなたか教えて頂けると幸いです。
なお、色も指定しているにも関わらず反映されませんので(Colaboratory画面、pdf共に)、対処法が分かれば教えてください。

import graphviz

g = graphviz.Digraph(format='pdf')
g.attr('node', shape='circle', fontname='IPAexGothic')
g.node('犬', fillcolor='blue')

g.attr('node', shape='ellipse', fontname='IPAexGothic')
g.node('猫', fillcolor='red')

g.render('犬猫')

1 likes

1Answer

canvasでも類似した文字化けがあります。

# -*- coding: utf-8 -*-
import graphviz
g = graphviz.Digraph(format='pdf', encoding='utf-8', graph_attr=(('fontname', 'MS Gothic')),
node_attr=(('fontname', 'MS Gothic')))

フォントは実在する奴を探して下さい。
((...))の文法は自信なさすぎです。

PEP8では# -*- coding: utf-8 -*- は禁止?ですがエディタへの指示は有効では?そもそも、PEP8は指針(ガイドライン)のようなものと思っていました。

暇人xin居酒屋

1Like

Comments

  1. @th8687rr

    Questioner

    ご回答ありがとうございます。このサイトは読んでトライしたのですが、うまくいきませんでした。Google Colaboratoryの環境とは違う環境なのが問題なのか、、頂いたコードをColaboratoryでinputすると、ValueError: dictionary update sequence element #0 has length 8; 2 is required というエラー表示がでました。

  2. g = graphviz.Digraph(format='pdf', encoding='utf-8')

    のみでエラーなく文字化けするでしょうか?
  3. MS Gothicは Google Colaboratoryにないので別のフォントを入れる必要があります。

    ValueError: dictionary update sequence element #0 has length 8; 2 is required

    は辞書関連のエラーみたいですよ!
    フォントとは無関係ではないですか?
  4. @th8687rr

    Questioner

    ご回答ありがとうございます!

    ① g = graphviz.Digraph(format='pdf', encoding='utf-8') ← 文字化けしました。
    ② MS Gothicは Google Colaboratoryにないので別のフォントを入れる必要があります。 ← これで成功しました。

    具体的には、https://qiita.com/nkay/items/b2d50349a3f5d38df45b このサイトにある1を行う事で成功しました。

    ありがとうございました!

Your answer might help someone💌