热门标签 | 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



推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
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社区 版权所有