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



推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 如何在窗口右下角添加调整大小的手柄
    本文探讨了如何在传统MFC/Win32 API编程中实现类似C# WinForms中的SizeGrip功能,即在窗口的右下角显示一个用于调整窗口大小的手柄。我们将介绍具体的实现方法和相关API。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 资源推荐 | TensorFlow官方中文教程助力英语非母语者学习
    来源:机器之心。本文详细介绍了TensorFlow官方提供的中文版教程和指南,帮助开发者更好地理解和应用这一强大的开源机器学习平台。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • ServiceStack与Swagger的无缝集成指南
    本文详细介绍了如何在ServiceStack项目中集成Swagger,以实现API文档的自动生成和在线测试。通过本指南,您将了解从配置到部署的完整流程,并掌握如何优化API接口的开发和维护。 ... [详细]
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社区 版权所有