python中matplotlib绘图中文显示问题
2013-08-23 11:30
991 查看
matplotlib是支持unicode编码的,出现图1的问题主要是没有找到合适的中文字体,解决方法有两个:
1.直接修改配置文件matplotlibrc
这种方法我没有试过,因为我安装的是python(x,y),配置文件放的地方不一定一致,所以就选择了下面的方法
2.在代码中动态设置(推荐方式)
这种方式不需要修改配置文件,比较方便,推荐该方法,下面是具体步骤:
首先要再python脚本中的开头加上后面的内容:#-*- coding: utf-8 -*-,即用utf8编码
然后在代码中动态设置字体,下面是主要的几行代码
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
plt.xlabel(u"电压差(V)", fontproperties=font)
plt.ylabel(u"介质损耗角差(度)", fontproperties=font)
plt.title(u"介质损耗角和荷电状态SOC关系图",fontproperties=font)
并且确保要输出的中文是unicode形式,检查是否为unicode代码:isinstance(s, unicode)。
给个效果图:
下面举个具体的例子,因为我在网上看了很多例子,都解决不了这个问题,为了方便大家,下面贴出代码,需要的话可以直接贴过去运行:
#-*- coding: utf-8 -*- from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) plt.figure(figsize=(6,6)) x = [1,2,3,4,5,6,7,8] y = [] for i in x: y.append(-(i*i)+i+3) plt.plot(x, y) plt.title(u'测试程序', fontproperties=font) plt.xlabel(u'x轴', fontproperties=font) plt.ylabel(u'y轴', fontproperties=font) plt.grid(True) plt.show()
下面是程序的输出
转自:http://blog.chinaunix.net/uid-26611383-id-3521248.html 感谢原作者
相关文章推荐
- Python中,matplotlib绘图无法显示中文的问题
- python中matplotlib绘图中文显示问题
- Python 中,matplotlib绘图无法显示中文的问题
- 解决python中matplotlib绘图中文显示问题
- Python 中,matplotlib绘图无法显示中文的问题
- Python绘图问题:Matplotlib中如何正确显示中文
- Python使用matplotlib绘图无法显示中文问题的解决方法
- python中matplotlib绘图中文显示问题
- python中matplotlib绘图中文显示问题
- Python 中,matplotlib绘图无法显示中文的问题
- python中matplotlib绘图中文显示问题
- python matplotlib绘图时图例显示问题
- 解决python27 matplotlib 中文不显示问题
- python使用matplotlib绘图时图例显示问题的解决
- python matplotlib中显示中文问题
- python matplotlib绘图时图例显示问题
- 解决python27 matplotlib 中文不显示问题
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- 解决windows下 Python中 matplotlib 做图中文不显示的问题
- Python 3下使用Matplotlib工具画图,中文显示乱码的问题解决(修改配置文件)