1. 程式人生 > >python學習(三):matplotlib學習

python學習(三):matplotlib學習

前言:matplotlib是一個python的第三方庫,裡面的pyplot可以用來作圖。下面來學習一下如何使用它的資源。

一、使用前

首先在python中使用任何第三方庫時,都必須先將其引入。即:

import matplotlib.pyplot as plt

或者:

from matplotlib.pyplot import *

二、用法

1.建立空白圖

fig = plt.figure()

得到如下圖的效果:
圖片上方—–(這裡由於圖是空白的所以看不見內容)——————————–

figure1
圖片下方——–(這裡由於圖是空白的所以看不見內容)———————————-

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

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

效果如下:
圖片上方—–(這裡由於圖是空白的所以看不見內容)——————————–

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()

效果如下:

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()

效果如下:

figure4

可以看到圖中的x,y軸座標都是從0到1,當然有時候我們需要其他的座標起始值。
此時可以使用語句指定:

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

或者:

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

效果如下:

figure5

注意第一個子圖。

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()

效果如下:

scatter

這裡的引數意義:

  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()

效果如下:
plot

這裡有一個新的引數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()

效果如下:

sin(x)

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()

效果如下:

pie

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()

效果如下:

bar

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")

效果圖:

2D

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()

效果圖:
others

三、結束語

儘管上述例子給出了基本的畫圖方法,但是其中的函式還有很多其他的用法(引數可能不只如此),因此本文只能算做一個基本入門。還需要參考API進行詳盡的知識學習。

四、參考

上述內容部分引用自: