A tutorial Devert Alexandre

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA

SCHOOL OF SOFTWARE ENGINEERING OF USTC

Matplotlib

A tutorial

Devert Alexandre

School of Software Engineering of USTC

30 November 2012 -- Slide 1/44

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA

Table of Contents

1 First steps 2 Curve plots 3 Scatter plots 4 Boxplots 5 Histograms 6 Usage example

SCHOOL OF SOFTWARE ENGINEERING OF USTC

Devert Alexandre (School of Software Engineering of USTC) -- Matplotlib -- Slide 2/44

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA

Curve plot

SCHOOL OF SOFTWARE ENGINEERING OF USTC

Let's plot a curve

import math import matplotlib . pyplot as plt

# Generate a sinusoid nbSamples = 256 xRange = (-math . p i , math . p i )

x, y = [] , [] for n in xrange ( nbSamples ):

k = (n + 0.5) / nbSamples x . append ( xRange [ 0 ] + ( xRange [ 1 ] - xRange [ 0 ] ) k) y . append ( math . s i n ( x [ -1]))

# Plot the sinusoid plt . plot (x , y) p l t . show ()

Devert Alexandre (School of Software Engineering of USTC) -- Matplotlib -- Slide 3/44

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA

Curve plot

This will show you something like this

SCHOOL OF SOFTWARE ENGINEERING OF USTC

1.0

0.5

0.0

0.5

1.0 4

3

2

10

1

2

3

4

Devert Alexandre (School of Software Engineering of USTC) -- Matplotlib -- Slide 4/44

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF CHINA

numpy

matplotlib can work with numpy arrays

SCHOOL OF SOFTWARE ENGINEERING OF USTC

import math import numpy import matplotlib . pyplot as plt

# Generate a sinusoid nbSamples = 256 xRange = (-math . p i , math . p i )

x , y = numpy . z e r o s ( nbSamples ) , numpy . z e r o s ( nbSamples ) for n in xrange ( nbSamples ):

k = (n + 0.5) / nbSamples x [ n ] = xRange [ 0 ] + ( xRange [ 1 ] - xRange [ 0 ] ) k y [ n ] = math . s i n ( x [ n ] )

# Plot the sinusoid plt . plot (x , y) p l t . show ()

numpy provides a lot of function and is efficient

Devert Alexandre (School of Software Engineering of USTC) -- Matplotlib -- Slide 5/44

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download