如何在 pandas 情节中显示中文? [英] How to display Chinese in pandas plot?

查看:75
本文介绍了如何在 pandas 情节中显示中文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我有一个与大熊猫有关的情节工作,像这样:

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是具有索引的数据帧的对象,我想要一个具有两列的简单二维图,一个是'title',另一个是'active_span'.

most_active_posts is 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'.

title是字符串类型,其中包含汉字,而active_span是整数类型.

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

如何正常显示汉字?

推荐答案

我的解决方法是这样的:

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.

这篇关于如何在 pandas 情节中显示中文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆