热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python画图程序库_python绘图matplotlib绘图库入门

简介:matplotlib是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图。而且也可以方

简介:matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图。而且也可以方便地将它作为绘

图控件,嵌入GUI应用程序中。

在Python中使用matplotlib.pyplot快速绘图

下面是matplotlib库所给的介绍

"""

This is an object-oriented plotting library.

这是面向对象的绘图库

A procedural interface is provided by the companion pyplot module,(程序接口直接import matplotlib.pyplot as plt 就可以了,或者使用ipython)

which may be imported directly, e.g.::

import matplotlib.pyplot as plt

or using ipython::

ipython

at your terminal, followed by::

In [1]: %matplotlib

In [2]: import matplotlib.pyplot as plt

at the ipython shell prompt.

For the most part, direct use of the object-oriented library is

encouraged when programming; pyplot is primarily for working

interactively. The

exceptions are the pyplot commands :func:`~matplotlib.pyplot.figure`,

:func:`~matplotlib.pyplot.subplot`,

:func:`~matplotlib.pyplot.subplots`, and

:func:`~pyplot.savefig`, which can greatly simplify scripting.

Modules include: (matplotlib模块里面有axes,figure,artist,lines,........)

:mod:`matplotlib.axes`

defines the :class:`~matplotlib.axes.Axes` class. Most pylab

commands are wrappers for :class:`~matplotlib.axes.Axes`

methods. The axes module is the highest level of OO access to

the library.

:mod:`matplotlib.figure`

defines the :class:`~matplotlib.figure.Figure` class.

:mod:`matplotlib.artist`

defines the :class:`~matplotlib.artist.Artist` base class for

all classes that draw things.

:mod:`matplotlib.lines`

defines the :class:`~matplotlib.lines.Line2D` class for

drawing lines and markers

:mod:`matplotlib.patches`

defines classes for drawing polygons

:mod:`matplotlib.text`

defines the :class:`~matplotlib.text.Text`,

:class:`~matplotlib.text.TextWithDash`, and

:class:`~matplotlib.text.Annotate` classes

:mod:`matplotlib.image`

defines the :class:`~matplotlib.image.AxesImage` and

:class:`~matplotlib.image.FigureImage` classes

:mod:`matplotlib.collections`

classes for efficient drawing of groups of lines or polygons

:mod:`matplotlib.colors`

classes for interpreting color specifications and for making

colormaps

:mod:`matplotlib.cm`

colormaps and the :class:`~matplotlib.image.ScalarMappable`

mixin class for providing color mapping functionality to other

classes

:mod:`matplotlib.ticker`

classes for calculating tick mark locations and for formatting

tick labels

:mod:`matplotlib.backends`

a subpackage with modules for various gui libraries and output

formats

The base matplotlib namespace includes:

:data:`~matplotlib.rcParams`

a global dictionary of default configuration settings. It is

initialized by code which may be overridded by a matplotlibrc

file.

:func:`~matplotlib.rc`

a function for setting groups of rcParams values

:func:`~matplotlib.use`

a function for setting the matplotlib backend. If used, this

function must be called immediately after importing matplotlib

for the first time. In particular, it must be called

**before** importing pylab (if pylab is imported).

matplotlib was initially written by John D. Hunter (1968-2012) and is now

developed and maintained by a host of others.

Occasionally the internal documentation (python docstrings) will refer

to MATLAB®, a registered trademark of The MathWorks, Inc.

"""阅读后,我们明白matplotlib实际上是一套面向对象的绘图库,它所绘制的图表中的每个绘图元素,例如线条Line2D、文字Text、刻度等在内存中都有一个对象与之对应。

我们只需要调用pyplot模块所提供的函数就可以实现快速绘图以及设置图表的各种细节。

def sca(ax):

"""

Set the current Axes instance to *ax*.

The current Figure is updated to the parent of *ax*.

"""

managers = _pylab_helpers.Gcf.get_all_fig_managers()

for m in managers:

if ax in m.canvas.figure.axes:

_pylab_helpers.Gcf.set_active(m)

m.canvas.figure.sca(ax)

return

raise ValueError("Axes instance argument was not found in a figure.")

def gcf():

"Get a reference to the current figure."

figManager = _pylab_helpers.Gcf.get_active()

if figManager is not None:

return figManager.canvas.figure

else:

return figure()为了将面向对象的绘图库包装成只使用函数的调用接口,pyplot模块的内部保存了当前图表以及当前子图等信息。当前的图表和子图可以使用plt.gcf()和plt.gca()获得,分别表示"Get Current Figure"和"Get Current Axes"。在pyplot模块中,许多函数都是对当前的Figure或Axes对象进行处理,比如说:

plt.plot()实际上会通过plt.gca()获得当前的Axes对象ax,然后再调用ax.plot()方法实现真正的绘图。

20161218103400863

20161218104939791

20161218105002117

绘制多子图(快速绘图)

Matplotlib 里的常用类的包含关系为 Figure -> Axes -> (Line2D, Text, etc.)一个Figure对象可以包含多个子图(Axes),在matplotlib中用Axes对象表示一个绘图区域,可以理解为子图。

可以使用subplot()快速绘制包含多个子图的图表,它的调用形式如下:

subplot(numRows, numCols, plotNum)

subplot将整个绘图区域等分为numRows行* numCols列个子区域&#xff0c;然后plotNum(1<&#61;plotNum<&#61;4且plotNum必须为正整数)按照从左到右&#xff0c;从上到下的顺序对每个子区域进行编号&#xff0c;左上的子区域的编号为1。如果numRows&#xff0c;numCols和plotNum这三个数都小于10的话&#xff0c;可以把它们缩写为一个整数&#xff0c;例如subplot(323)和subplot(3,2,3)是相同的&#xff0c;其中最后一位的3表示在第三象限画图的。subplot在plotNum指定的区域中创建一个轴对象。如果新创建的轴和之前创建的轴重叠的话&#xff0c;之前的轴将被删除。

20161218111754456

20161218112800056

&#39;&#39;&#39; 为了方便快速绘图matplotlib通过pyplot模块提供了一套和MATLAB类似的绘图API&#xff0c;

将众多绘图对象所构成的复杂结构隐藏在这套API内部。

我们只需要调用pyplot模块所提供的函数就可以实现快速绘图以及设置图表的各种细节 &#39;&#39;&#39;

&#39;&#39;&#39; plt.plot()实际上会通过plt.gca()获得当前的Axes对象ax&#xff0c;然后再调用ax.plot()方法实现真正的绘图。 &#39;&#39;&#39;

def learn2():

plt.figure(1) # 创建图表1

plt.figure(2) # 创建图表2

ax1 &#61; plt.subplot(211) # 在图表2中创建子图1

ax2 &#61; plt.subplot(212) # 在图表2中创建子图2

x &#61; np.linspace(0, 3, 100)

for i in xrange(5):

plt.figure(1) # 选择图表1

# plt.plot(x, np.exp(i * x / 3), &#39;o&#39;)

plt.sca(ax1) # 将子图1放进for厘面的plt中

plt.plot(x, np.sin(i * x))

plt.sca(ax2)

plt.plot(x, np.cos(i * x))

plt.show()参考来自&#xff1a;

http://www.voidcn.com/article/p-kdngefxg-bp.html



推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 记录一些 Latex 的技巧
    Latex一些技巧:1.如何创建不浮动的的figure和table\makeatletter\newcommand{\figcaption}{\def\captyp ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了如何使用MATLAB调用摄像头进行人脸检测和识别。首先需要安装扩展工具,并下载安装OS Generic Video Interface。然后使用MATLAB的机器视觉工具箱中的VJ算法进行人脸检测,可以直接调用CascadeObjectDetector函数进行检测。同时还介绍了如何调用摄像头进行人脸识别,并对每一帧图像进行识别。最后,给出了一些相关的参考资料和实例。 ... [详细]
  • SmartRefreshLayout自定义头部刷新和底部加载
    1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s ... [详细]
  • PS网页设计教程VIII——在Photoshop中设计不同寻常布局
    作为编码者,美工基础是偏弱的。我们可以参考一些成熟的网页PS教程,提高自身的设计能力。套用一句话,“熟读唐诗三百首,不会作诗也会吟”。本系列的教程来源于网上的PS教程,都是国外的,全英文的。本人尝 ... [详细]
author-avatar
灬段裝丶緈褔_998
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有