Japanese notation in matplotlib in networkx

Asked 2 years ago, Updated 2 years ago, 89 views

This is a rudimentary point, but how can I use networkx to put matplotlib in Japanese?
I imported Japanese-matplotlib, but it turned into tofu.
I would appreciate your advice.

#coding=UTF-8

# function declaration
import networkx as nx
import string
import pandas aspd
import collections
import itertools
# import matplotlib.pyplot asplt
import numpy as np
import numpy.linalg as LA
import matplotlib.pyplot asplt
import japanize_matplotlib
# Specify directed graph
G=nx.DiGraph()
# Create edge (side) list by loading files
G=nx.read_edgelist("sm.knif.prn", nodetype=str, create_using=nx.DiGraph(), encoding="utf-8")

# Number of nodes (vertex) output
print(nx.number_of_nodes(G))
# Edge Count Output
print(nx.number_of_edges(G))
# Network Basic Information Output
print(nx.info(G))
# order distribution
print(nx.degre_histogram(G))
# Calculate proximity centrality
pr=nx.betweenness_centrality(G)

plt.figure(figsize=(50,50))

# weighting using proximity centrality
nx.draw_kamada_kawai(G,node_color=list(pr.values())),cmap=plt.cm.Reds,node_size=[1000000*v for vin pr.values(),with_labels=True,font_family=IPAexGothic)


plt.axis("off")
plt.show()

python matplotlib networkx

2022-09-30 21:45

1 Answers

It seems to have been resolved in the comments, but it will be made into an answer with subsequent supplement

The IPAex font seems to have been copied to the matplotlib folder to resolve the issue, but the direct cause is a coding error with parameters.

#Weighting using proximity centrality
nx.draw_kamada_kawai(G,node_color=list(pr.values())),
    cmap=plt.cm.Reds, node_size=[1000000*v for vin pr.values(),
    with_labels=True, font_family=IPAexGothic)

I folded it back because it was long.
This is because the font name of this last font_family=IPAexGothic is not enclosed in a single quotation.Simply set it to font_family='IPAexGothic' to display Japanese.

From a little earlier stage, if there is a font in the original font list of matplotlib, you don't need japanize_matplotlib or IPAexGothic, for example, Windows can use font_family='MS Gothic'.

matplotlib and networkx do not appear to be reflected in the networkx side of the feature, but matplotlib If you specify the following Japanese font at the beginning of the program, you do not need to specify the font individually.

import matplotlib

and then

matplotlib.rcParams ['font.family'] = 'MS Gothic'

Or

matplotlib.rc('font', family='MS Gothic')

Run the 'MS Gothic' section, specifying the desired Japanese displayable font.

It seems that MacOS and Linux/Unix have chocolate problems, so it would be better to install IPA fonts.

Reference
Make matplotlib display compatible in Japanese by simply installing pip and importing
Make Japanese fonts available on matplotlib in macOS
Remove Japanese garbled characters in matplotlib (Windows Edition)
How to view Japanese easily in Matplotlib (Windows)
How to Avoid Matplotlib/networkX Tofu in an Environment Without sudo Permission

IPA font compatible with the peace of mind> IPAex font Ver.004.01
IPAmj tomorrow morning font


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.