Python编程深度学习绘图库之matplotlib


Posted in Python onDecember 28, 2018

matplotlib是python的一个开源的2D绘图库,它的原作者是John D. Hunter,因为在设计上借鉴了matlab,所以成为matplotlib。和Pillow一样是被广泛使用的绘图功能,而在深度学习相关的部分,matplotlib得宠的多。这篇文章将简单介绍一下如何安装以及使用它来画一些非常常见的统计图形。

概要信息

Python编程深度学习绘图库之matplotlib

注意事项:由于Python2支持到2020年,很多python库都开始主要支持python3了,matplotlib的主分支也已经是python3了。而这篇文章中为了简单,安装和demo代码依然是在python2.7上进行验证的。

安装

使用pip install即可直接安装。安装日志如下:

liumiaocn:tmp liumiao$ python -mpip install -U pip
Requirement already up-to-date: pip in /usr/local/lib/python2.7/site-packages (10.0.1)
liumiaocn:tmp liumiao$ 
liumiaocn:tmp liumiao$ python -mpip install -U matplotlib
Collecting matplotlib
 Downloading https://files.pythonhosted.org/packages/61/38/d70e8bf77d5cb27d5f3595edd0b3978825063feadd023786d2591e393e6e/matplotlib-2.2.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (13.7MB)
  100% |????????????????????????????????| 13.7MB 2.1MB/s 
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/6a/8a/718fd7d3458f9fab8e67186b00abdd345b639976bc7fb3ae722e1b026a50/pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
  100% |????????????????????????????????| 61kB 5.8MB/s 
Collecting backports.functools-lru-cache (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/03/8e/2424c0e65c4a066e28f539364deee49b6451f8fcd4f718fefa50cc3dcf48/backports.functools_lru_cache-1.5-py2.py3-none-any.whl
Collecting subprocess32 (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/fa/60/b50459f291cae6bc1d0ff711b75e5130684fd3949370fdba78f6c57c1903/subprocess32-3.5.2-cp27-cp27m-macosx_10_6_intel.whl
Collecting pytz (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
  100% |????????????????????????????????| 512kB 8.9MB/s 
Requirement not upgraded as not directly required: six>=1.10 in /usr/local/lib/python2.7/site-packages (from matplotlib) (1.11.0)
Collecting python-dateutil>=2.1 (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
  100% |????????????????????????????????| 215kB 7.2MB/s 
Collecting kiwisolver>=1.0.1 (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/79/d8/94633718f3f77dcb638687a77ba199325a1cb158d2d4b00c9dc17f2b5c72/kiwisolver-1.0.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (110kB)
  100% |????????????????????????????????| 112kB 5.9MB/s 
Collecting cycler>=0.10 (from matplotlib)
 Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Requirement not upgraded as not directly required: numpy>=1.7.1 in /usr/local/lib/python2.7/site-packages (from matplotlib) (1.14.5)
Requirement not upgraded as not directly required: setuptools in /usr/local/lib/python2.7/site-packages (from kiwisolver>=1.0.1->matplotlib) (39.2.0)
Installing collected packages: pyparsing, backports.functools-lru-cache, subprocess32, pytz, python-dateutil, kiwisolver, cycler, matplotlib
Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 pyparsing-2.2.0 python-dateutil-2.7.3 pytz-2018.4 subprocess32-3.5.2
liumiaocn:tmp liumiao$

确认

liumiaocn:~ liumiao$ pip show matplotlib
Name: matplotlib
Version: 2.2.2
Summary: Python plotting package
Home-page: http://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires: pyparsing, backports.functools-lru-cache, subprocess32, pytz, six, python-dateutil, kiwisolver, cycler, numpy
Required-by: 
liumiaocn:~ liumiao$

使用

正弦函数

我们使用numpy和matplotlib可以非常简单地就能够画出正弦的波形图,具体代码如下:

liumiaocn:plot liumiao$ cat plot-1.py 
#!/usr/local/bin/python
import numpy as np
import matplotlib.pyplot as plot
print("y=sin(x): x with range fo -4pi to 4pi")
x=np.arange(-4*np.pi, 4*np.pi, 0.1)
y=np.sin(x)
#print sin
plot.plot(x,y)
plot.show()
liumiaocn:plot liumiao$

执行&结果确认

liumiaocn:plot liumiao$ python plot-1.py 
y=sin(x): x with range fo -4pi to 4pi

Python编程深度学习绘图库之matplotlib

直方图

最简单的直方图的做法,可以通过bar或者hist来创建

liumiaocn:plot liumiao$ cat plot-2.py 
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(7)
y = (10,15,24,7,14,30,20)
plt.bar(x,y)
plt.show()
liumiaocn:plot liumiao$

Python编程深度学习绘图库之matplotlib

饼图

使用pie函数创建饼图

liumiaocn:plot liumiao$ cat plot-3.py 
#!/usr/local/bin/python
import matplotlib.pyplot as plt
dateinf = '06/01', '06/02', '06/03', '06/04', '06/05', '06/06', '06/07'
bugnums = [11,22,7,29,24,15,18]
plt.pie(bugnums, labels=dateinf)
plt.show()
liumiaocn:plot liumiao$

Python编程深度学习绘图库之matplotlib

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
python小技巧之批量抓取美女图片
Jun 06 Python
用PyQt进行Python图形界面的程序的开发的入门指引
Apr 14 Python
Python实现的Excel文件读写类
Jul 30 Python
Python使用Mechanize模块编写爬虫的要点解析
Mar 31 Python
Python探索之URL Dispatcher实例详解
Oct 28 Python
python3.5绘制随机漫步图
Aug 27 Python
BP神经网络原理及Python实现代码
Dec 18 Python
Python中py文件转换成exe可执行文件的方法
Jun 14 Python
python-xpath获取html文档的部分内容
Mar 06 Python
Selenium之模拟登录铁路12306的示例代码
Jul 31 Python
降低python版本的操作方法
Sep 11 Python
pytest实现多进程与多线程运行超好用的插件
Jul 15 Python
对pandas写入读取h5文件的方法详解
Dec 28 #Python
Python线程池模块ThreadPoolExecutor用法分析
Dec 28 #Python
Python实现的逻辑回归算法示例【附测试csv文件下载】
Dec 28 #Python
python 检查是否为中文字符串的方法
Dec 28 #Python
浅谈python3发送post请求参数为空的情况
Dec 28 #Python
python3使用flask编写注册post接口的方法
Dec 28 #Python
python通过tcp发送xml报文的方法
Dec 28 #Python
You might like
php cli 小技巧
2013/06/03 PHP
php简单统计在线人数的方法
2016/05/10 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
2016/09/21 PHP
Maps Javascript
2007/01/22 Javascript
打造基于jQuery的高性能TreeView(asp.net)
2011/02/23 Javascript
Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
2011/10/29 Javascript
UI Events 用户界面事件
2012/06/27 Javascript
jQuery实现首页图片淡入淡出效果的方法
2015/06/10 Javascript
深入浅析JavaScript中with语句的理解
2016/05/12 Javascript
JavaScript toUpperCase()方法使用详解
2016/08/26 Javascript
Node.JS中事件轮询(Event Loop)的解析
2017/02/25 Javascript
Node.js使用Express创建Web项目详细教程
2017/03/31 Javascript
5分钟打造简易高效的webpack常用配置
2017/07/04 Javascript
Node.js实现连接mysql数据库功能示例
2017/09/15 Javascript
angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
2018/08/29 Javascript
Vue中rem与postcss-pxtorem的应用详解
2019/11/20 Javascript
微信小程序实现canvas分享朋友圈海报
2020/06/21 Javascript
jQuery实现简单全选框
2020/09/13 jQuery
[53:15]2018DOTA2亚洲邀请赛3月29日 小组赛A组 KG VS OG
2018/03/30 DOTA
[01:37]PWL S2开团时刻DAY1&2——这符有毒
2020/11/20 DOTA
python pdb调试方法分享
2014/01/21 Python
Python实现图片转字符画的示例代码
2017/08/21 Python
TF-IDF与余弦相似性的应用(二) 找出相似文章
2017/12/21 Python
python生成圆形图片的方法
2020/03/25 Python
Python 12306抢火车票脚本
2018/02/07 Python
python 判断文件还是文件夹的简单实例
2019/06/10 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
Matplotlib自定义坐标轴刻度的实现示例
2020/06/18 Python
Prometheus开发中间件Exporter过程详解
2020/11/30 Python
一款纯css3实现的竖形二级导航的实例教程
2014/12/11 HTML / CSS
爱尔兰电脑、家电和家具购物网站:Buy It Direct
2019/07/09 全球购物
25道Java面试题集合
2013/05/21 面试题
公司会计主管岗位责任制
2014/03/01 职场文书
平安建设工作方案
2014/06/02 职场文书
药店采购员岗位职责
2014/09/30 职场文书
初中生思想道德自我评价
2015/03/09 职场文书