python学习(三):matplotlib学习

来源:互联网 发布:flask mysql 编辑:程序博客网 时间:2024/06/12 20:45

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

一、使用前

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

import matplotlib.pyplot as plt
  • 1
  • 1

或者:

from matplotlib.pyplot import *
  • 1
  • 1

二、用法

1.建立空白图

fig = plt.figure()
  • 1
  • 1

得到如下图的效果: 
图片上方—–(这里由于图是空白的所以看不见内容)——————————–

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

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

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

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

figure2 
图片下方——–(这里由于图是空白的所以看不见内容)———————————- 
当然我们也可以建立一个包含多个子图的图,使用语句:

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

效果如下:

figure3

其中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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下:

figure4

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

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

或者:

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

效果如下:

figure5

注意第一个子图。

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

首先给出一组数据:

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

A.画散点图*

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

效果如下:

scatter

这里的参数意义:

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

    b---blue   c---cyan  g---green    k----blackm---magenta r---red  w---white    y----yellow
    • 1
    • 2
    • 1
    • 2

    有四种表示颜色的方式:

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

    .  Point marker,  Pixel markero  Circle markerv  Triangle down marker ^  Triangle up marker <  Triangle left marker >  Triangle right marker 1  Tripod down marker2  Tripod up marker3  Tripod left marker4  Tripod right markers  Square markerp  Pentagon marker*  Star markerh  Hexagon markerH  Rotated hexagon D Diamond markerd  Thin diamond marker| Vertical line (vlinesymbol) marker_  Horizontal line (hline symbol) marker+  Plus markerx  Cross (x) marker
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下: 
plot

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

-      实线--     短线-.     短点相间线:     虚点线
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

另外除了给出数据画图之外,我们也可以利用函数表达式进行画图,例如: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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

效果如下:

sin(x)

C.扇形图

示例:

import matplotlib.pyplot as plty = [2.3, 3.4, 1.2, 6.6, 7.0]plt.figure()plt.pie(y)plt.title('PIE')plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果如下:

pie

D.柱状图bar

示例:

import matplotlib.pyplot as pltx = [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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

效果如下:

bar

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

import matplotlib.pyplot as pltimport numpy as npimport matplotlib.image as mpimg# 2D datadelta = 0.025x = y = np.arange(-3.0, 3.0, delta)X, Y = np.meshgrid(x, y)Z = Y**2 + X**2plt.figure(figsize=(12, 6))plt.subplot(121)plt.contour(X, Y, Z)plt.colorbar()plt.title("contour")# read imageimg=mpimg.imread('marvin.jpg')plt.subplot(122)plt.imshow(img)plt.title("imshow")plt.show()#plt.savefig("matplot_sample.jpg")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

效果图:

2D

F.对所画图进行补充

__author__ = 'wenbaoli'import matplotlib.pyplot as pltfrom 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 axisplt.ylabel(u'Sin(X)')#fill the meaning of Y axisplt.title(u'sin(x)')#add the title of the figureplt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

效果图: 
others

三、结束语

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

四、参考

上述内容部分引用自:

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