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 ...
随机推荐
- ubuntu安装ntp时间服务器
1.安装ntp软件 sudo apt-get install ntp2.修改配置文件 sudo vim /etc/ntp.conf driftfile /var/lib/ntp/ntp.dr ...
- java线程状态 以及 sheep()、wait()、yield() 区别
前言 最近看到很多人都在讨论多线程的问题,于是写出了这篇博客,希望可以帮到正在学习和使用这块的朋友们,首先我们先看看两个图(两个图都来自其他码农的分享) 这两个图是一样的逻辑,这里一起罗列出来,下 ...
- 关系型数据库MySQL多实例
简介 MySQL数据库是一个中小型关系型数据库管理系统,软件开发者为瑞典MySQL AB公司.在2008年1月16号被Sun公司收购后Sun公司又被oracle公司收购.目前MySQL被广泛地应用在I ...
- Linux Ubuntu系统下Java开发环境搭建
操作系统:Linux x64 / Ubuntu 14.04 Java JDK版本:jdk-8u65-linux-x64.tar.gz 声明:转载请注明出处及本文链接 1. 前往ORACLE官网下载最新 ...
- redis数据类型(五)set类型
一. set类型 set是无序集合,最大可以包含(2 的 32 次方-1)个元素. set 的是通过 hash table 实现的,所以添加,删除,查找的复杂度都是 O(1). hash table ...
- spring cloud连载第二篇之Spring Cloud Config
Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...
- C# SocketUdpServer
public interface ISocketUdpServer { void Start(); void Stop(); int SendData(byte[] data, IPEndPoint ...
- 使用PowerShell为SSAS Role添加Membership
PowerShell, SSAS, Role, Membership 上篇随笔使用PowerShell创建SSAS Role贴出了如何使用PowerShell批量创建Role,由于个人项目需求,创建R ...
- jQuery基础---常规选择器
内容摘要: 1.简单选择器 2.进阶选择器 3.高级选择器 发文不易,转载请注明出处! jQuery 最核心的组成部分就是:选择器引擎.它继承了 CSS 的语法,可以对 DOM 元素的标签名.属性名. ...
- Bundle传递数据,Handler更新UI
Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean.byte.in ...