如何在matplotlib plot中显示中文 [英] How to display Chinese in matplotlib plot

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

问题描述

在这里,我有一个与熊猫有关的情节工作,如下所示:

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-dimensional 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 .

如何才能正常显示汉字?

How can I display Chinese characters normally?

推荐答案

我的解决方法是这样的:

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.

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

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