matplotlib.pyplot 导引
matplotlib.pyplot 是采用 python 语言和使用数值数学库 numpy 数组数据的绘图库.其主要目标是用于数据的可视化显示.
输出图形组成
matplotlib.pyplot 模块中,其绘制的输出图形 (Figure) 的各组成部分,如下图所示
其中
Figure 是整个输出图形,记录了所有子 Axes 对象,一些特殊的 artists (如标题,图例等) 和画布 (canvas).画布 (canvas) 对用户是不可见的.
Axes 是含有数据空间的图像区域.一个 figure 可以包含多个 Axes,但一个给定的 Axes 只能在一个 Figure 中.注意 Axes 与 Axis 的区别,Axis是指数学上的坐标轴,Axes可以包含 2 个 (二维) 或 3 个坐标轴 (三维).在 matplotlib 模块中,matplotlib.axes.Axes类,包含大多数的图形元素,如坐标轴 (matplotlib.axis.Axis), 刻度 (matplotlib.axis.Tick), 二维线段 (matplotlib.lines.Line2D), 文本 (matplotlib.text.Text), 多边形 (matplotlib.patches.Polygon),并设置坐标系统;matplotlib.pyplot.axes(arg=None, **kwargs) 函数向当前的图形 (current figure) 中,增加一个 Axes 对象,并设置其为当前的 Axes 对象;matplotlib.pyplot.axis(*v, **kwargs) 函数用于获取或设置坐标轴属性.
在 Figure 上的任何对象,基本都是 Artist.当渲染图形时,所有的 Artists 都会画到画布 (canvas) 上.大多数 Artists 都绑定到一个 Axes 上,不能被多个 Axes 共享,或从一个 Axes 移动到另一个.
编码风格
使用 matplotlib.pyplot 模块,通常有两种编码风格,一种是采用面向对象接口 (object-oriented interface) 的编码风格,另一种是类似 MATLAB (MATLAB-like) 的编码风格.对于复杂的图形绘制,推荐采用面向对象接口的编码风格.
- 面向对象编码风格的示例代码,如下所示.
import matplotlib.pyplot as plt
import numpy as np x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
plt.show()
上述代码中,使用 pyplot 创建和显示图形,剩下的使用对象方法实现,其中 ax 对象是 Axes 类的对象.对于 matplotlib.pyplot 模块,Axes 类和它的成员函数使用面向对象接口的主要入口点.
- 类似 MATLAB 的编码风格的示例代码,如下所示
import matplotlib.pyplot as plt
import numpy as np x = np.arange(0, 10, 0.2)
y = np.sin(x)
plt.figure()
plt.subplot(111)
plt.plot(x, y)
plt.show()
pylab 和 pyplot 关系
matplotlib.pyplot 是 matplotlib 模块中的一个模块;pylab 是安装 matplotlib 模块时附带安装的模块.
pylab 是一个带来便利的模块,其可以在单一的命名空间 (pylab namespace) 下导入用于绘图的 matplotlib.pyplot 模块和用于数值数学和操作数组的 numpy 模块.虽然有许多例子使用 pylab,但不再推荐使用.其示例代码,如下所示:
import pylab x = pylab.arange(0, 10, 0.2)
y = pylab.sin(x)
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
pylab.show()
参考资料
1. Usage - The Matplotlib FAQ. https://matplotlib.org/faq/usage_faq.html.
2. matplotlib.pyplot - The Matplotlib API. https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html.
3. pyplot summary - The Matplotlib API. https://matplotlib.org/api/pyplot_summary.html.
4. Axes class - The Matplotlib API. https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes
5. matplotlib.pyplot.axes - matplotlib.pyplot. https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axes.html#matplotlib.pyplot.axes
6. matplotlib.pyplot.axis - matplotlib.pyplot. https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axis.html#matplotlib.pyplot.axis
7. Pyplot tutorials - Tutorials. https://matplotlib.org/tutorials/introductory/pyplot.html.
matplotlib.pyplot 导引的更多相关文章
- matplotlib.pyplot 绘图详解 matplotlib 安装
apt-get install python-matplotlib 转载自: http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086. ...
- matplotlib.pyplot.hist
**n, bins, patches = plt.hist(datasets, bins, normed=False, facecolor=None, alpha=None)** ## 函数说明 用于 ...
- 数据分析之matplotlib.pyplot模块
首先都得导模块. import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import S ...
- Python Matplotlib.pyplot plt 中文显示
话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...
- 在绘图的时候import matplotlib.pyplot as plt报错:ImportError: No module named '_tkinter', please install the python-tk package
在绘图的时候import matplotlib.pyplot as plt报错:ImportError: No module named '_tkinter', please install the ...
- Matplotlib.pyplot 把画图保存为图片
在plt.show()之前执行plt.savefig()函数即可. 简单例子: import matplotlib.pyplot as plt x=[1,2,3,4,5] y=[10,5,15,10, ...
- 使用numpy与matplotlib.pyplot画图
使用numpy与matplotlib.pyplot画图 1. 折线图 1 # -*- enccoding:utf-8 -*- 2 import numpy as np 3 import matplot ...
- 用matplotlib.pyplot画简单的折线图,直方图,散点图
#coding=utf-8 """ 用matplotlib.pyplot画简单的折线图,直方图,散点图 """ import matplot ...
- matplotlib.pyplot展示MNIST图片
import torch import torch.utils.data as Data import torchvision import torchvision.transforms as tra ...
随机推荐
- Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
解决方法: find / -name supervisor.sock unlink /name/supervisor.sock 2. www-data 用户是干什么用的 3.如何通过superviso ...
- 学习Linux最简单的方法
大多数初学者在刚刚接触Linux都会有非常陌生的感觉.往往会有一些疑惑和问题.而我们就沿着这些问题,从远及近,从宏观到微观来理解Linux的简洁和美丽. 问题1:Winows有注册表,为什么Linux ...
- CentOS6.4将MySQL5.1升级至5.5.36
1.为了安全期间,首先需要备份原有数据 2.卸载原有MySQL,先停止原有的MySQL服务,再查找 find / -name mysql [root@qxyw /]# find / -name mys ...
- nginx源码编译以及源码编译过程中遇到的问题
本文主要讲nginx安装以及安装过程中遇到的问题. 谈到nginx 必须聊聊它的起源和发展. nginx是由俄罗斯工程师Igor Sysoev 用C语言开发的一个免费开源的Web服务器软件,于2004 ...
- InnoDB索引概述,二分查找法,平衡二叉树
索引是应用程序设计和开发的一个重要方面.如果索引太多,应用的性能可能会受到影响:如果索引太少,对查询性能又会产生影响.要找到一个合适的平衡点,这对应用的性能至关重要. 如果知道数据的使用,从一开始就应 ...
- memcached 学习笔记 4
memcached真实项目中的应用 1 缓存式的Web应用程序架构 有了缓存的支持,我们可以在传统的app层和db层之间加入cache层,每个app服务器都可以绑定一个mc, 每次数据的读取都可以从m ...
- Django url分发到工程里
因为我们建立了Django后 ,url是在mysite下的全局对象 因为我们实际项目里不可能只有一个工程 而全放在全局里去分发url 会让代码耦合度提高,代码量大后会造成维护困难.这时候我们把url分 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- jquery选择器【总结】
本文总结整理了jquery里和选择器相关的所有方法,通过这篇文章,可以让你学习到在jquery里使用选择器的所有方法. 一:基本选择器: $("#aijquery") 选择id值等 ...
- 使用k8s创建容器一直处于ContainerCreating状态
容器报错信息为(两种): FailedSynError syncing pod, skipping: failed to {kubelet 127.0.0.1} Warning FailedSync ...