前言:matplotlib是一个python的第三方库,里面的pyplot可以用来作图。下面来学习一下如何使用它的资源。

一、使用前

首先在python中使用任何第三方库时,都必须先将其引入。即:

import matplotlib.pyplot as plt

或者:

from matplotlib.pyplot import *

二、用法

1.建立空白图

fig = plt.figure()

得到如下图的效果:

图片上方—–(这里由于图是空白的所以看不见内容)——————————–



图片下方——–(这里由于图是空白的所以看不见内容)———————————-

也可以指定所建立图的大小

fig = plt.figure(figsize=(4,2))

效果如下:

图片上方—–(这里由于图是空白的所以看不见内容)——————————–



图片下方——–(这里由于图是空白的所以看不见内容)———————————-

当然我们也可以建立一个包含多个子图的图,使用语句:

plt.figure(figsize=(12,6))
plt.subplot(231)
plt.subplot(232)
plt.subplot(233)
plt.subplot(234)
plt.subplot(235)
plt.subplot(236)
plt.show()

效果如下:

其中subplot()函数中的三个数字,第一个表示Y轴方向的子图个数,第二个表示X轴方向的子图个数,第三个则表示当前要画图的焦点。

当然上述写法并不是唯一的,比如我们也可以这样写:

fig = plt.figure(figsize=(6, 6))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
plt.show()

效果如下:

可以看到图中的x,y轴坐标都是从0到1,当然有时候我们需要其他的坐标起始值。

此时可以使用语句指定:

ax1.axis([-1, 1, -1, 1])

或者:

plt.axis([-1, 1, -1, 1])

效果如下:

注意第一个子图。

2.向空白图中添加内容,想你所想,画你所想

首先给出一组数据:

x = [1, 2, 3, 4, 5]
y = [2.3, 3.4, 1.2, 6.6, 7.0]

A.画散点图*

plt.scatter(x, y, color='r', marker='+')
plt.show()

效果如下:

这里的参数意义:

  1. x为横坐标向量,y为纵坐标向量,x,y的长度必须一致。
  2. 控制颜色:color为散点的颜色标志,常用color的表示如下:

    b---blue   c---cyan  g---green    k----black
    m---magenta r---red w---white y----yellow

    有四种表示颜色的方式:

    • 用全名
    • 16进制,如:#FF00FF
    • 灰度强度,如:‘0.7’
  3. 控制标记风格:marker为散点的标记,标记风格有多种:

    .  Point marker
    , Pixel marker
    o Circle marker
    v Triangle down marker
    ^ Triangle up marker
    < Triangle left marker
    > Triangle right marker
    1 Tripod down marker
    2 Tripod up marker
    3 Tripod left marker
    4 Tripod right marker
    s Square marker
    p Pentagon marker
    * Star marker
    h Hexagon marker
    H Rotated hexagon D Diamond marker
    d Thin diamond marker
    | Vertical line (vlinesymbol) marker
    _ Horizontal line (hline symbol) marker
    + Plus marker
    x Cross (x) marker

B.函数图(折线图)

数据还是上面的。

fig = plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.plot(x, y, color='r', linestyle='-')
plt.subplot(122)
plt.plot(x, y, color='r', linestyle='--')
plt.show()

效果如下:

这里有一个新的参数linestyle,控制的是线型的格式:符号和线型之间的对应关系

-      实线
-- 短线
-. 短点相间线
: 虚点线

另外除了给出数据画图之外,我们也可以利用函数表达式进行画图,例如:y=sin(x)

from math import *
from numpy import *
x = arange(-math.pi, math.pi, 0.01)
y = [sin(xx) for xx in x]
plt.figure()
plt.plot(x, y, color='r', linestyle='-.')
plt.show()

效果如下:

C.扇形图

示例:

import matplotlib.pyplot as plt
y = [2.3, 3.4, 1.2, 6.6, 7.0]
plt.figure()
plt.pie(y)
plt.title('PIE')
plt.show()

效果如下:

D.柱状图bar

示例:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2.3, 3.4, 1.2, 6.6, 7.0] plt.figure()
plt.bar(x, y)
plt.title("bar")
plt.show()

效果如下:

E.二维图形(等高线,本地图片等)

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg
# 2D data delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = Y**2 + X**2
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.contour(X, Y, Z)
plt.colorbar()
plt.title("contour") # read image img=mpimg.imread('marvin.jpg') plt.subplot(122)
plt.imshow(img)
plt.title("imshow")
plt.show()
#plt.savefig("matplot_sample.jpg")

效果图:

F.对所画图进行补充

__author__ = 'wenbaoli'

import matplotlib.pyplot as plt
from math import *
from numpy import *
x = arange(-math.pi, math.pi, 0.01)
y = [sin(xx) for xx in x]
plt.figure()
plt.plot(x, y, color='r', linestyle='-')
plt.xlabel(u'X')#fill the meaning of X axis
plt.ylabel(u'Sin(X)')#fill the meaning of Y axis
plt.title(u'sin(x)')#add the title of the figure plt.show()

效果图:

三、结束语

尽管上述例子给出了基本的画图方法,但是其中的函数还有很多其他的用法(参数可能不只如此),因此本文只能算做一个基本入门。还需要参考API进行详尽的知识学习。

四、参考

上述内容部分引用自:

http://www.cnblogs.com/vamei/archive/2013/01/30/2879700.html

python学习(三):matplotlib学习的更多相关文章

  1. Python 第三天学习整理①

    今天学习的内容包括以下几个方面:1.字符编码 2.文件操作 3.函数 1.字符编码 关于字符编码,关注以下几个点 1.1 什么是字符编码 字符编码:字符转化为数字的过程所遵循的标准 字符编码包括:un ...

  2. python数据分析之matplotlib学习

    本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 类MATLAB API 最简单的入门是从类 MATLAB API 开始,它被设计成兼容 MATLAB 绘图函数. from p ...

  3. Python 第三天学习整理2

    一.常用的字符串方法 capitalize() #将字符串首字母大写 center(100,'*') #把字符串居中的 count(‘zhou’)#查询次数 endswith('.jpg ')#判断字 ...

  4. python库之matplotlib学习---关于坐标轴

    首先定·定义x, y创建一个figure import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1, 1, 10) y ...

  5. 系统学习python第三天学习笔记

    day02补充 运算符补充 in value = "我是中国人" # 判断'中国'是否在value所代指的字符串中. "中国"是否是value所代指的字符串的子 ...

  6. python库之matplotlib学习---图无法显示中文

    在代码前面加上 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] ...

  7. java_web学习(三) eclipse_jsp学习

    1.首先打开eclipse,新建一个Dynamac web project项目文件 2.在WebContent单击右键创建JSP File 3.过程 4.简单的jsp代码 运行结果: 5.导出war文 ...

  8. Html学习(三) 分类学习

    代码: <h1>这是一级分类吗</h1> <h2>这是二级分类吗</h2> <h3>这是三级分类吗 </h3> 效果: 介绍: ...

  9. 学习Python的三种境界

    前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...

  10. Python入门基础学习 三

    Python入门基础学习 三 数据类型 Python区分整型和浮点型依靠的是小数点,有小数点就是浮点型. e记法:e就是10的意思,是一种科学的计数法,15000=1.5e4 布尔类型是一种特殊的整形 ...

随机推荐

  1. Phonegap通过JS访问本地接口的两种方法

    Phonegap为跨设备的应用开发提供了一个解决方案.如果某个应用只有js和html,则可以通过Phonegap的在线build工具,编译出多个平台的app安装包.当然通过Phonegap提供的js可 ...

  2. Call to undefined function bcscale()

    参考官方文档发现zabbix需要bcmath函数库的支持,其中bcscale()就是该函数库中的函数之一. 因此,现在只需要让php支持bcmath即可. yum -y install php-bcm ...

  3. C#分析搜索引擎URL得到搜索关键字,并判断页面停留时间以及来源页面

    前台代码: var start; var end; var state; var lasturl = document.referrer; start = new Date($.ajax({ asyn ...

  4. IOS开发-UITextField代理常用的方法总结

    1.//当用户全部清空的时候的时候 会调用 -(BOOL)textFieldShouldClear:(UITextField *)textField: 2.//可以得到用户输入的字符 -(BOOL)t ...

  5. css样式单位取整,去掉'px'

    alert(parseInt($(".themes1").css("margin-left"), 10));

  6. bzoj2044: 三维导弹拦截

    Description 一场战争正在A国与B国之间如火如荼的展开. B国凭借其强大的经济实力开发出了无数的远程攻击导弹,B国的领导人希望,通过这些导弹直接毁灭A国的指挥部,从而取得战斗的胜利!当然,A ...

  7. [Issue]repo/repo init-解决同步源码Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle

    1. 前两天想搭建freescale L3.0.35_4.1.0_BSP包,结果LTIB环境搭建好,也编译出rootfs/uboot/kernel的Image了,但是准备移植uboot的时候发现ubo ...

  8. DBA_Oracle Archive Log的基本应用和启用(概念)

    2014-11-15 Created By BaoXinjian

  9. DBA_在Linux上安装Oracle Database11g数据库(案例)

    2014-08-08 Created By BaoXinjian

  10. 【初识】KMP算法入门(转)

    感觉写的很好,尤其是底下的公式,易懂,链接:http://www.cnblogs.com/mypride/p/4950245.html 举个例子 模式串S:a s d a s d a s d f a  ...