Matplotlib Quick Reference

Matplotlib Quick Reference

Matti Pastell, University of Helsinki.

Import pylab

from pylab import *

Line plots

x = linspace(0, 2*pi, 100) y = sin(x) y2 = cos(x) plot(x, y) plot(x, y2, 'og') title('sin(x) and cos(x)') xlabel('x') ylabel('f(x)') legend(['sin(x)', 'cos(x)'], loc = 3)

12

10

8

6

4

2

0

22

0

2

4

6

8

10

Bar plot

y = [9, 12, 7] errors = [1.3, 0.9, 2] x = arange(len(y)) bar(x, y, yerr=errors, ecolor="red",

capsize=10)

Fixed x-error and variable y-error

1.0

0.5

0.0

0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0

Histogram

x = randn(1000) hist(x, bins=20)

14

1.0

sin(x) and cos(x)

12

160

10

140

0.5

8

120

f(x)

0.0

6

100

4

80

0.5

sin(x)

cos(x)

1.00

1

2

3x4

5

6

7

2

00.0

0.5

1.0

1.5

2.0

2.5

3.0

Errorbars

60

40

20

04

3

2

10

1

2

3

4

Scatter plot

x = arange(0.1, 4, 0.5) y = exp(-x)

Boxplot

x = arange(10) y = x + randn(len(x))

y_error = 0.1 + 0.2*np.sqrt(x) x_error = 0.1

y2 = x + randn(len(x))

errorbar(x, y, xerr = x_error,

scatter(x, y, s=100, alpha=0.7)

yerr = y_error, fmt='o', ecolor="red") x = randn(100, 5)

scatter(x, y2, s=100, marker="d", color="red") title('Fixed x-error and variable y-error')

boxplot(x)

3

2

1

0

1

2

31

2

3

4

5

Stem plot

x = range(10) y = rand(10) stem(x, y)

1.0

0.8

0.6

0.4

0.2

0.00 1 2 3 4 5 6 7 8 9

Contour

x = linspace(-3, 3, 200) y=x X,Y = meshgrid(x, y) Z = bivariate_normal(X, Y, sigmaxy=0.5) contourf(X, Y, Z) colorbar() xlabel('X') ylabel('Y') title(r"$\sigma_{XY}$ = 0.5")

3

XY = 0.5

0.200

0.175 2

0.150 1

0.125

Y

0

0.100

0.075 1

0.050 2

0.025

33

2

10

1

2

3 0.000

X

Surface plot

from mpl_toolkits.mplot3d import Axes3D fig = figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap="jet")

0.20

0.15

0.10

0.05

0.00

3

2

3

2

10

1

2

3

1 0 1 2 3

Subplots

n = 128. x = arange(n)/n y = sin(0.125*pi*n*x**2) subplot(221) plot(x,y,'r') xlabel('x') ylabel(r'$sin(0.125n \cdot x^2)$') subplot(222)

x = arange(0, 2*pi, 0.2) y = sin(x) stem(x,y) axis([0, 2*pi, -1.2, 1.2]) xlabel('x') ylabel('sinx(x)') subplot(223) y = randn(1000) hist(y) xlabel('bin') ylabel('count') subplot(224) boxplot(y) ylabel('y')

sin(0.125n ?x2 )

1.0

1.0

0.5

0.5

sinx(x)

0.0

0.0

0.5

0.5

1.00.0 250

0.2

0.4 x 0.6

0.8

1.0 1.0 0

4

1

2

3x 4

5

6

200

3

2

150

1

y

100

0

50

1 2

03 2 1 0 1 2 3 4 3

1

bin

count

Links

? Matplotlib tutorial: ~rougier/teaching/matplotlib/

? Examples gallery: gallery.html

? Matplotlib colormaps 2013/05/02/matplotlib_colormaps/

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

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

Google Online Preview   Download