How to display Chinese in matplotlib plot
Asked Answered
C

5

4

Here, I have a plot work to do with pandas, like this :

most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')

most_active_posts is an object of dataframe with index, I want a simple two-dimensional plot with two columns, one is 'title' and the other is 'active_span'.

title is type of string, which contains Chinese characters, while active_span is type of integer .

How can I display Chinese characters normally?

Cerebrospinal answered 23/1, 2014 at 12:13 Comment(0)
S
5

My work-around is like this:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
font = fm.FontProperties(fname='c:\\windows\\fonts\\simsun.ttc')  # speicify font
ax = most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')
ax.set_xticklabels(most_active_posts['title'].str.decode('utf-8'), fontproperties=font)
plt.show()

Basically, you need to specify a valid font for Chinese characters.

Steinke answered 3/11, 2015 at 3:52 Comment(0)
A
6

I find a python library designed for fixing Chinese display in pip. You can download it using the command in your terminal:

pip install pyplotz

And you can write the following code instead(full code):

from pyplotz.pyplotz import PyplotZ
pltz = PyplotZ()
pltz.enable_chinese()
most_active_posts.plot(x='title',y='active_span',kind='bar')
pltz.xticks(np.arange(len(df.cn)),df.cn,rotation=360)
pltz.legend()
pltz.show()

The result is like this

And it can help you handle matplotlib Chinese font for you! This is the github page:

https://github.com/201528015329004/pyplotz

And there are some handy examples:

https://github.com/201528015329004/pyplotz/blob/master/examples/quick_start.ipynb

Atonsah answered 17/11, 2017 at 8:8 Comment(1)
this solution works for colab.research.google.com as wellGesso
S
5

My work-around is like this:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
font = fm.FontProperties(fname='c:\\windows\\fonts\\simsun.ttc')  # speicify font
ax = most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')
ax.set_xticklabels(most_active_posts['title'].str.decode('utf-8'), fontproperties=font)
plt.show()

Basically, you need to specify a valid font for Chinese characters.

Steinke answered 3/11, 2015 at 3:52 Comment(0)
H
2
  1. Enter the directory Lib\site-packages\matplotlib\mpl-data, and edit the file matplotlibrc.
  2. Locate font.family and font.sans-serif then uncomment both of them and inset Microsoft YaHei after font.sans-serif.
Herophilus answered 26/10, 2015 at 10:33 Comment(0)
H
0

I think you want the characters to be the labels on the plot right?

I just grabbed some random characters:

In [40]: df
Out[40]: 
   0 title
0  0     뉵
1  1     뉑
2  2     늘
3  3     度

[4 rows x 2 columns]

I don't think there is a way to set y_ticklabels from df.plot(), but you can set them from the returned axes object:

In [47]: ax = df.plot(kind='barh')

In [48]: ax.set_yticklabels(df['title'].str.decode('utf-8'))
Out[48]: 
[<matplotlib.text.Text at 0x1152abfd0>,
 <matplotlib.text.Text at 0x1152a3910>,
 <matplotlib.text.Text at 0x111c5e790>,
 <matplotlib.text.Text at 0x111c5ef10>]

In [49]: plt.draw()

Here's the figure:

enter image description here

I'm not actually able to save the file and have the characters show up. Not sure why at the moment, but this may get you started.

Hickey answered 23/1, 2014 at 14:37 Comment(1)
Hei,friend! I tried as you suggested ,unforunately problem still there . My situation much more like this : s1 = Series([u'\u3010\u676d\u5dde\u3011\u7f8e\u8d44\u901a\u4fe1\u516c\u53f8\u6025\u62db\u524d\u7aef\u5f00\u53d1\u5de5\u7a0b\u5e08',u'\u730e\u5934']) s2 = Series([15,16]) data = {'title':s1,'rate':s2} df = DataFrame(data) ax = df.plot(kind = 'barh') ax.set_yticklabels(df.title.str.decode('utf-8')) plt.draw()Cerebrospinal
S
0

if you use pandas, you can use get_xticklabels to get labels and then set them with set_xticklabels.

import matplotlib.font_manager as mfm
import matplotlib.pyplot as plt

font_path = "/System/Library/Fonts/STHeiti Light.ttc"
prop = mfm.FontProperties(fname=font_path)
df = pd.read_csv("data.txt"]
figure, ax = plt.subplots(figsize=(12, 4))
tmp = df.boxplot(by='shop', column='buy', ax=ax)
ax.set_xticklabels(tmp.get_xticklabels(), fontproperties=prop)
plt.show()
Susannesusceptibility answered 16/7, 2019 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.