How do I render multiple languages (e.g. Chinese, cyrillic, english) at once in a legend using matplotlib?

Viewed 192

I am plotting statistics related to different translations of a word. Using python / matplotlib under Ubuntu I cannot get my legend to show all my languages.

My code below

#!/usr/bin/python

import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('pgf') # This may make things harder; solutions without it are still of interest
# pip3 install matplotlib==3.5
plt.close('all')

fig,ax= plt.subplots(1)
ax.plot(1,1,'.',label='english')
ax.plot(1,2,'.',label='Glück')
ax.plot(1,1,'.',label='доход')
ax.plot(1,1,'.',label='幸福')
plt.legend()
plt.savefig('tmp.pdf')
plt.savefig('tmp.png')


print(matplotlib.__version__)

seems to do fine with various Unicode, but not Chinese. I get the following:

enter image description here

/home/meuser/tmpbug.py:23: UserWarning: Glyph 24184 (\N{CJK UNIFIED IDEOGRAPH-5E78}) missing from current font.
  
/home/meuser/tmpbug.py:23: UserWarning: Glyph 31119 (\N{CJK UNIFIED IDEOGRAPH-798F}) missing from current font.
  
/home/meuser/tmpbug.py:23: UserWarning: Glyph 24184 (\N{CJK UNIFIED IDEOGRAPH-5E78}) missing from current font.
  
/home/meuser/tmpbug.py:23: UserWarning: Glyph 31119 (\N{CJK UNIFIED IDEOGRAPH-798F}) missing from current font.
  
/home/meuser/tmpbug.py:24: UserWarning: Glyph 24184 (\N{CJK UNIFIED IDEOGRAPH-5E78}) missing from current font.
  print(plt.__version__)
/home/meuser/tmpbug.py:24: UserWarning: Glyph 31119 (\N{CJK UNIFIED IDEOGRAPH-798F}) missing from current font.
  print(plt.__version__)
3.5.0

How can I get all my labels to show in one legend? If this is not possible, why not?

1 Answers

Looking this problem up, I found this answer here that uses pyplotz. I adapted it to your problem and it seems to be solving your issue:

!pip install pyplotz
import matplotlib.pyplot as plt
import matplotlib

from pyplotz.pyplotz import PyplotZ
pltz = PyplotZ()
pltz.enable_chinese()


fig,ax= plt.subplots(1)
ax.plot(1,1,'.',label='english')
ax.plot(1,2,'.',label='Glück')
ax.plot(1,1,'.',label='доход')
ax.plot(1,1,'.',label='幸福')

pltz.legend()

And the output gives:

enter image description here

This was tested on google colab with matplotlib 3.2.2 and 3.5.0

EDIT:

Here is a way to create two legends one for the chinese characters and the second one for the other languages. Here is the code:

import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
from pyplotz.pyplotz import PyplotZ
pltz = PyplotZ()
pltz.enable_chinese()


fig,ax= plt.subplots(1)

e,=ax.plot(1,1,'.',label='english')
g,=ax.plot(1,2,'.',label='Glück')
r,=ax.plot(1,1,'.',label='доход')
c,=ax.plot(1,1,'.',label='幸福')
legend=plt.legend([e,g,r],['english','Glück','доход'],loc='best',bbox_to_anchor=(0.5, 0., 0.5, 0.5),frameon=False)
legend1=pltz.legend([c],['幸福'],loc='best',bbox_to_anchor=(0.452, -0.21, 0.5, 0.5),frameon=False)
plt.gca().add_artist(legend)

And the output gives:

enter image description here

Related