How to using Japanese characteristic in plot

Hi every body,

I am very new about the Julia and Juliabox also.
I am making a graphs using PyPlot on Juliabox.
I want to set Legend and axes labels in Japanese.
But it is not work at all, the Japanese park is shown as rectangular box.

Could any one help me to solve this problem?
PS. I do not install any program in my computer. All I do that is in the Google Chrome .

Thank you very much.

Hi,

I think this is one of the most difficult questions to solve remotely because many factors are involved in the problem. You can find numerous blog posts about it if you google it in Japanese. But I’d like to help you as much as I can.

First, let’s try the following script. The third line specifies the font family used in your plot.

using PyPlot

rc("font", family = "Noto Sans CJK JP") 
x = range(0, 2π, step = 0.2)
plot(x, sin.(x), label = "サイン波")
plot(x, cos.(x), label = "コサイン波")
legend()
title("日本語タイトル")
xlabel("x軸")
ylabel("y軸")
savefig("plot.png")

If you are lucky, you can see something like this.
plot

If you see some errors, you may not have the font used in the example above. The font name is “Noto Sans CJK JP”, which covers almost all Japanese characters. The font file can be downloaded from https://www.google.com/get/noto/. Alternatively, you can specify your favorite font name instead of it. If you are using macOS or Windows, I believe several Japanese fonts are available out of box.

If it still doesn’t work, matplotlib may refer to a staled font file cache. This seems to happen when you install matplotlib before fonts. You can remove the cache file and try it again. On Linux, the cache file is located in ~/.cache/matplotlib/. See here for more details about the cache file location.

If you are using some old matplotlib, several Japanese fonts may not be usable because the support of TrueType Collection or TTC, in which most Japanese fonts are encoded, was introduced recently (Support (first font of) TTC files. by anntzer · Pull Request #9787 · matplotlib/matplotlib · GitHub). I’m using matplotlib 3.1.1, the latest version of today; you can check the version you’re using as follows:

julia> using PyPlot

julia> matplotlib.__version__
"3.1.1"

If you try all above and it still doesn’t work, I’m sorry I don’t know why. However, more information about your environment (specifically, operating system, python versions, available fonts, etc.) would be useful to help you.

4 Likes

Ah, I missed you wrote you used JuliaBox. In that case, installing new fonts may be difficult. You can list all available fonts as follows. If you can find a Japanese font there, you can use it instead of Noto Sans CJK JP.

julia> using PyPlot

julia> matplotlib.font_manager.fontManager.ttflist
440-element Array{PyCall.PyObject,1}:
 PyObject <Font 'STIXSizeTwoSym' (STIXSizTwoSymReg.ttf) normal normal regular normal>
 PyObject <Font 'STIXNonUnicode' (STIXNonUni.ttf) normal normal regular normal>
 PyObject <Font 'STIXGeneral' (STIXGeneralItalic.ttf) italic normal 400 normal>
 PyObject <Font 'STIXSizeFourSym' (STIXSizFourSymBol.ttf) normal normal bold normal>
 PyObject <Font 'DejaVu Sans' (DejaVuSans-Oblique.ttf) oblique normal 400 normal>
 PyObject <Font 'cmex10' (cmex10.ttf) normal normal 400 normal>
 PyObject <Font 'DejaVu Serif' (DejaVuSerif-BoldItalic.ttf) italic normal bold normal>
 PyObject <Font 'STIXSizeOneSym' (STIXSizOneSymBol.ttf) normal normal bold normal>
 PyObject <Font 'DejaVu Sans Mono' (DejaVuSansMono.ttf) normal normal 400 normal>
 PyObject <Font 'STIXSizeThreeSym' (STIXSizThreeSymBol.ttf) normal normal bold normal>
 PyObject <Font 'STIXGeneral' (STIXGeneralBolIta.ttf) italic normal bold normal>
 ⋮
 PyObject <Font 'Noto Serif Lao' (NotoSerifLao-Bold.ttf) normal normal bold normal>
 PyObject <Font 'Noto Sans Ol Chiki' (NotoSansOlChiki-Regular.ttf) normal normal regular normal>
 PyObject <Font 'Noto Sans Thai' (NotoSansThai-Regular.ttf) normal normal regular normal>
 PyObject <Font 'Noto Sans Linear B' (NotoSansLinearB-Regular.ttf) normal normal regular normal>
 PyObject <Font 'Lohit Telugu' (Lohit-Telugu.ttf) normal normal 400 normal>
 PyObject <Font 'Noto Sans Adlam Unjoined' (NotoSansAdlamUnjoined-Regular.ttf) normal normal regular normal>
 PyObject <Font 'Noto Sans Meetei Mayek' (NotoSansMeeteiMayek-Regular.ttf) normal normal 400 normal>
 PyObject <Font 'Tlwg Typewriter' (TlwgTypewriter-BoldOblique.ttf) oblique normal bold normal>
 PyObject <Font 'Noto Sans Mono' (NotoSansMono-Regular.ttf) normal normal regular normal>
 PyObject <Font 'Purisa' (Purisa-Oblique.ttf) oblique normal 400 normal>
1 Like

Hi Bicycle 1885 (Kenta Sato san)

Thank you very much for your reply.
I have searched about this problem by Japanese, however, most of the suggestions are install the font or edit matplotlibrc file.
But is you mentioned, it is not possible when I am using Juliabox without install anything.
I would like to try ti find the Japanese font in the JuliaBox, but it is huge data coming.
Could you please teach me how to filter only Japanese fonts?

Thank and Best regards,

1 Like

I looked over the font list but unfortunately I couldn’t find any promising font. However, I found a workaround. You can specify FontProperties like below:

# Download a Japanese font.
download("https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip", "NotoSansCJKjp-hinted.zip")
run(`unzip NotoSansCJKjp-hinted.zip NotoSansCJKjp-Regular.otf`)

# Initialize FontProperties.
fontprops = matplotlib[:font_manager][:FontProperties](fname = "NotoSansCJKjp-Regular.otf")

# Pass the FontProperty object to title, xlabel, ylabel etc.
x = range(0, 2π, step = 0.1)
plot(x, sin.(x))
title("日本語タイトル", fontproperties = fontprops)
xlabel("x軸", fontproperties = fontprops)
ylabel("y軸", fontproperties = fontprops);

I know this is hacky and this may be an unrecommended way. But it works.

6 Likes

Hi bicycle 1885

With your help, I can make my graphs with the legend in Japanese perfectly.
Thank you very much.

I had an example here for PGFPlotsX which could be modified:

Incidentally, if someone happens to be using pgfplots with CJK characters, contributing an example at

would be appreciated.

1 Like