例子6、中文标签测试

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath)
#定义一个myfont变量, myfont = matplotlib.font_manager.FontProperties(fname=fontpath); fontpath就是字体文件的路径
x = np.arange(1,5)
plt.plot(x,x*3.0,x,x*1.5,x,x/3.0) plt.grid(True) #添加背景方格
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) plt.savefig('test3.png')

测试效果:

参考文献

http://hi.baidu.com/bithigher/item/b9ce6d85dc102adc98255fb7

例子7、添加图例

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*- import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath) x = np.arange(1,5)
#设置legend,图例说明
plt.plot(x, x*1.5, label = "Normal")
plt.plot(x, x*3.0, label = "Fast")
plt.plot(x, x/3.0, label = "Slow") plt.grid(True)
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) #Place a legend on the current axes
#设置图例显示的位置
plt.legend(loc='upper left')
#Save the current figure
plt.savefig('test4.png')

输出效果:

对于图例的其他位置,如下:

          ===============   =============
Location String Location Code
=============== =============
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
=============== =============

可以选择best,默认upper right

可以批量添加legend,但是必须和plot对应:

In [4]: plt.plot(x, x*1.5)
In [5]: plt.plot(x, x*3.0)
In [6]: plt.plot(x, x/3.0)
In [7]: plt.legend(['Normal', 'Fast', 'Slow'])

例子8、输出图像

相关函数:

#Save the current figure
plt.savefig('test4.png')

输出图像的格式:

[root@typhoeus79 20131114]# file test4.png
test4.png: PNG image data, 800 x 600, 8-bit/color RGBA, non-interlaced

并且按照文件扩展名来区分,默认分辨率是800*600

两个参数控制输出图像的大小:

1、figure size

mpl.rcParams['figure.figsize'] = (16,9)

2、DPI

In [1]: import matplotlib as mpl

In [2]: mpl.rcParams['figure.figsize']
Out[2]: [8.0, 6.0] In [3]: mpl.rcParams['savefig.dpi']
Out[3]: 100
matplotlib.rcParams
An instance of RcParams for handling default matplotlib values.

http://matplotlib.org/1.3.1/api/matplotlib_configuration_api.html?highlight=rcparams#matplotlib.RcParams

改变分辨率:

plt.savefig('test4.png',dpi=200)
[root@typhoeus79 20131114]# file test4.png
test4.png: PNG image data, 1600 x 1200, 8-bit/color RGBA, non-interlaced

例子9、输出为其他格式

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*-
import matplotlib as mpl
mpl.use('Agg')#before importing pyplot
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/chinese/TrueType/ukai.ttf'
myfont = fm.FontProperties(fname=fontpath) x = np.arange(1,5)
plt.plot(x, x*1.5, label = "Normal")
plt.plot(x, x*3.0, label = "Fast")
plt.plot(x, x/3.0, label = "Slow") plt.grid(True)
plt.xlabel(u'X轴',fontproperties=myfont)
plt.ylabel(u'Y轴',fontproperties=myfont)
plt.title(u'中文测试',fontproperties=myfont) plt.legend(loc='best') #以文件名后缀作为区分
plt.savefig('test4.pdf',dpi=500)

或者PS,SVG其他格式都可以。

例子10 使用GTK

>>> import matplotlib as mpl
>>> mpl.use('GTKAgg') # to use GTK UI
>>>
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,3,2,4])
[<matplotlib.lines.Line2D object at 0x02ED3630>]
>>> plt.show()

输出结果:

(待续)

Python之matplotlib学习(二)的更多相关文章

  1. Python入门基础学习 二

    Python入门基础学习 二 猜数字小游戏进阶版 修改建议: 猜错的时候程序可以给出提示,告诉用户猜测的数字偏大还是偏小: 没运行一次程序只能猜测一次,应该提供多次机会给用户猜测: 每次运行程序,答案 ...

  2. Python的数据处理学习(二)

    本文参考Paul Barry所著的<Head First Python>一书,参考代码均可由http://python.itcarlow.ie/站点下载.本文若有任何谬误希望不吝赐教~ 二 ...

  3. Python之matplotlib学习(一)

    小试牛刀 在上一节已经安装好matplotlib模块,下面使用几个例子熟悉一下. 对应的一些文档说明: http://matplotlib.org/1.3.1/api/pyplot_summary.h ...

  4. Python之matplotlib学习(四)

    例子12:ipython使用--pylab参数,默认加入matplotlib模块 [root@typhoeus79 guosong]# ipython --pylab WARNING: IPython ...

  5. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  6. Python之matplotlib学习(三)

    例子11-1:横坐标时间的处理 from matplotlib.dates import datestr2num,DateFormatter import matplotlib.dates as da ...

  7. Python - 3.6 学习二

    Python 的高级特性 切片 对于指定索引范围取值的操作,Python提供了slice方法,类似于Excel中数据透视表的切片器. >>> L = ['Michael', 'Sar ...

  8. Python:2D画图库matplotlib学习总结

    本文为学习笔记----总结!大部分为demo.一部分为学习中遇到的问题总结.包含怎么设置标签为中文等.matlab博大精深.须要用的时候再继续吧. Pyplot tutorial Demo地址为:点击 ...

  9. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

随机推荐

  1. 项目常用Javascript分享,包含常用验证和Cookie操作

    function IsEmail(str) { var r = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if (r.test(str)) ...

  2. 51nod 1593 公园晨跑 | ST表(线段树?)思维题

    51nod 1593 公园晨跑 有一只猴子,他生活在一个环形的公园里.有n棵树围绕着公园.第i棵树和第i+1棵树之间的距离是 di ,而第n棵树和第一棵树之间的距离是 dn .第i棵树的高度是 hi ...

  3. 【特效】hover效果之四线动画

    效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/3102.htm html: <div class="wrap&q ...

  4. nginx URL重写

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  5. Emgu.CV(三)

    像素交换 private void btn_Exchange_Click(object sender, EventArgs e) { if (imageBox1.Image != null) { va ...

  6. 微信小程序城市定位(借助百度地图API判断城市)

    概述 微信小程序提供一些API(地址)用于获取当前用户的地理位置等信息,但无论是wx.getLocation,还是wx.chooseLocation均没有单独的字段表示国家与城市信息,仅有经纬度信息. ...

  7. 在 Arch 下编译 OpenWRT cmcurl 问题与解决方案

    0 现象 在 Arch 下编译 OpenWRT (15.05) cmcurl 时报错: [ 28%] Linking C executable LIBCURL lib/libcmcurl.a(open ...

  8. 斐讯 FIR151M 频繁掉线(OpenWRT解决方案)

    0. 现象与前言 在使用斐讯 FIR151M 路由器连接网络时,传输数据时频繁掉线. 官方固件刷了两个版本,问题未解决. 建议高级用户看本教程,要做好不能使用 Web 管理界面的心理准备. 1. 准备 ...

  9. 使用vsftpd+nginx搭建一个文件服务器

    一:安装vsftpd 1.安装 [root@localhost jack]# yum -y install vsftpd 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp ...

  10. web开发|如何选择合适的webui框架

    在市场中很多人分不清框架和库的区别,部分只知道框架模糊的概念.所以在选择webUI框架的时候就会仁者见仁智者见智,会存在各抒己见也是很正常的,这里整体都叫框架吧,在市场中不断的淘汰与创新,主要以Vue ...