pandas 如何在熊猫图中显示中文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21307832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Chinese in pandas plot?
提问by joe
Here, I have a plot work to do with pandas, like this :
在这里,我有一个与Pandas有关的情节工作,如下所示:
most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')
most_active_postsis an object of dataframe with index, I want a simple two-dimentional plot with two columns, one is 'title'and the other is 'active_span'.
most_active_posts是一个带索引的数据框对象,我想要一个简单的二维图,有两列,一个是'title',另一个是'active_span'。
titleis type of string, which contains Chinese characters, while active_spanis type of integer .
title是字符串类型,包含汉字,active_span而是整数类型。
How can I display Chinese characters normally?
怎样才能正常显示汉字?
回答by zaxliu
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.
基本上,您需要为中文字符指定有效的字体。
回答by Ray
I find a python library designed for fixing Chinese display in pip. You can download it using the command in your terminal:
我找到了一个专为修复pip中的中文显示而设计的python库。您可以使用终端中的命令下载它:
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()
And it can help you handle matplotlib Chinese font for you! This is the github page:
它可以帮你处理matplotlib中文字体!这是github页面:
https://github.com/201528015329004/pyplotz
https://github.com/201528015329004/pyplotz
And there are some handy examples:
还有一些方便的例子:
https://github.com/201528015329004/pyplotz/blob/master/examples/quick_start.ipynb
https://github.com/201528015329004/pyplotz/blob/master/examples/quick_start.ipynb
回答by cyy
- Enter the directory
Lib\site-packages\matplotlib\mpl-data, and edit the filematplotlibrc. - Locate
font.familyandfont.sans-serifthen uncomment both of them and insetMicrosoft YaHeiafterfont.sans-serif.
- 进入目录
Lib\site-packages\matplotlib\mpl-data,编辑文件matplotlibrc。 - 找到
font.family并font.sans-serif取消注释它们并Microsoft YaHei在 之后插入font.sans-serif。
回答by zhaoqing
if you use pandas, you can use get_xticklabelsto get labels and then set them with set_xticklabels.
如果您使用Pandas,则可以使用get_xticklabels获取标签,然后使用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()
回答by TomAugspurger
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_ticklabelsfrom df.plot(), but you can set them from the returned axesobject:
我认为没有办法设置y_ticklabelsfrom df.plot(),但您可以从返回的axes对象中设置它们:
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:
这是图:


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.
我实际上无法保存文件并显示字符。目前不知道为什么,但这可能会让你开始。

