Plot Python
-
Upload
vitalianoperezquispe -
Category
Documents
-
view
220 -
download
0
description
Transcript of Plot Python
-
Graficas en Python
Graficas estadstica y minera de datos con python
Miguel Cardenas Montes
Centro de Investigaciones Energeticas Medioambientales y Tecnologicas,Madrid, Spain
22-26 de Abril de 2013
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 1 / 24
-
Tabla de Contenidos
1 Objectivos
2 Introduccion
3 Una Grafica Basica
4 Histograma
5 Boxplot
6 Multiples graficas
7 Aumentando la Complejidad
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 2 / 24
-
Objectivos
Realizacion de graficas y analisis simples.
Aspectos Tecnicos
scipy
matplotlib
numpy
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 3 / 24
-
Introduccion
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 4 / 24
-
Lo que los cientficos necesitan
Manipular y procesar datos.
Visualizar resultados para entender que se esta haciendo.
Comunicar resultados: producir graficas para publicaciones, informes,
presentaciones, etc.
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 5 / 24
-
Ladrillos
En Python existe una coleccion de herramientas que cubre la mayor
parte de las necesidades cientficas.
Los tiempos de desarrollos disminuyen considerablemente.
Gran capacidad de reutilizacion y comparticion de codigo existente.
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 6 / 24
-
Grafica basica I
Grafica basica relacionando valores en coordenadas y abcisas.
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# show the plot on the screen
pl.show()
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
0
5
10
15
20
25
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 7 / 24
-
Grafica basica II: Puntos en vez de lnea.
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y as red circles
pl.plot(x, y, ro)
# show the plot on the screen
pl.show()
1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
0
5
10
15
20
25
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 8 / 24
-
Grafica basica III: Colores
Indicador Color
b blue - azul
g green - verde
r reed - rojo
c cyan -
m magenta -
y yellow - amarillo
k black - negro
w white - blanco
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 9 / 24
-
Grafica basica IV: Estilo de la lnea
pl.plot(x, y, o)
Indicador Estilo
- solid line style
dashed line style
-. dash-dot line style
: dotted line style
. point marker
s square marker
p pentagon marker
* star marker
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 10 / 24
-
Grafica basica V: Enriqueciendo la grafica
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# give plot a title
pl.title(Plot of y vs. x)
# make axis labels
pl.xlabel(x axis)
pl.ylabel(y axis)
# set axis limits
pl.xlim(0.0, 7.0)
pl.ylim(0.0, 30.)
# show the plot on the screen
pl.show()
0 1 2 3 4 5 6 7
x axis
0
5
10
15
20
25
30
y axis
Plot of y vs. x
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 11 / 24
-
Grafica basica VI: Mas de un dibujo por grafica
import numpy as np
import pylab as pl
# Make x, y arrays for each graph
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
# use pylab to plot x and y
pl.plot(x1, y1, r)
pl.plot(x2, y2, g)
# give plot a title
pl.title(Plot of y vs. x)
# make axis labels
pl.xlabel(x axis)
pl.ylabel(y axis)
# set axis limits
pl.xlim(0.0, 9.0)
pl.ylim(0.0, 30.)
# show the plot on the screen
pl.show()
0 1 2 3 4 5 6 7 8 9
x axis
0
5
10
15
20
25
30
y axis
Plot of y vs. x
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 12 / 24
-
Grafica basica VII: Leyenda
import numpy as np
import pylab as pl
# Make x, y arrays for each graph
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
# use pylab to plot x and y : Give your plots names
plot1 = pl.plot(x1, y1, r)
plot2 = pl.plot(x2, y2, go)
# give plot a title
pl.title(Plot of y vs. x)
# make axis labels
pl.xlabel(x axis)
pl.ylabel(y axis)
# set axis limits
pl.xlim(0.0, 9.0)
pl.ylim(0.0, 30.)
# make legend
pl.legend([plot1, plot2], (red line, green circles), best, numpoints=1)
# show the plot on the screen
pl.show()
0 1 2 3 4 5 6 7 8 9
x axis
0
5
10
15
20
25
30
y axis
Plot of y vs. x
red line
green circles
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 13 / 24
-
Grafica basica VIII: Leyenda
pl.legend((plot1, plot2), (label1, label2), best, numpoints=1)
El primer parametro es la lista
de dibujos.
El segundo es la lista de
etiquetas.
El tercero es la posicion de las
etiquetas: upper right, upper
left, center, lower left, lower
right, best
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 14 / 24
-
Histograma I: uno simple
import numpy as np
import pylab as pl
# make an array of random numbers with
#a gaussian distribution with
# mean = 5.0
# rms = 3.0
# number of points = 1000
data = np.random.normal(5.0, 3.0, 1000)
# make a histogram of the data array
pl.hist(data)
# make plot labels
pl.xlabel(data)
pl.show()
5 0 5 10 15 20
data
0
50
100
150
200
250
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 15 / 24
-
Histograma II: variar el tamano del bin
import numpy as np
import pylab as pl
# make an array of random numbers with
#a gaussian distribution with
# mean = 5.0
# rms = 3.0
# number of points = 1000
data = np.random.normal(5.0, 3.0, 1000)
# make a histogram of the data array
pl.hist(data)
bins = np.arange(-5., 16., 1.)
pl.hist(data, bins, histtype=stepfilled)
# make plot labels
pl.xlabel(data)
pl.show()
5 0 5 10 15
data
0
50
100
150
200
250
300
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 16 / 24
-
Histograma II: semitransparente
pl.hist(data)
pl.hist(data, alpha=0.3)
pl.hist(data, bins, histtype=stepfilled)
pl.hist(data, bins, histtype=stepfilled, alpha=0.3)
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 17 / 24
-
Histograma III: Anadir textos
import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, \
facecolor=g, alpha=0.75)
plt.xlabel(Smarts)
plt.ylabel(Probability)
plt.title(Histogram of IQ)
plt.text(60, .025, r$\mu=100,\ \sigma=15$)
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()40 60 80 100 120 140 160
Smarts
0.000
0.005
0.010
0.015
0.020
0.025
0.030
Probability
=100,=15
Histogram of IQ
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 18 / 24
-
Boxplot I
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 19 / 24
-
Boxplot II
import numpy as np
from pylab import *
#gaussian distributions
dataset_1 = np.random.normal(5.0, 3.0, 1000)
dataset_2 = np.random.normal(5.0, 5.0, 1000)
dataset_3 = np.random.normal(4.0, 1.0, 1000)
datos=[dataset_1, dataset_2, dataset_3]
xticks( arange(3), (Dataset 1, Dataset 2, \
Dataset 3) )
boxplot(datos)
show()Dataset 1 Dataset 2 Dataset 3
15
10
5
0
5
10
15
20
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 20 / 24
-
Multiples graficas I
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), bo, t2, f(t2), k)
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), r--)
plt.show()
0 1 2 3 4 5
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
0 1 2 3 4 5
1.0
0.5
0.0
0.5
1.0
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 21 / 24
-
Aumentando la Complejidad I
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)
nullfmt = NullFormatter() # no labels
# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left+width+0.02
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 22 / 24
-
Aumentando la Complejidad II
# start with a rectangular Figure
plt.figure(1, figsize=(8,8))
axScatter = plt.axes(rect_scatter)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)
# no labels
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)
# the scatter plot:
axScatter.scatter(x, y)
# now determine nice limits by hand:
binwidth = 0.25
xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y))] )
lim = ( int(xymax/binwidth) + 1) * binwidth
axScatter.set_xlim( (-lim, lim) )
axScatter.set_ylim( (-lim, lim) )
bins = np.arange(-lim, lim + binwidth, binwidth)
axHistx.hist(x, bins=bins)
axHisty.hist(y, bins=bins, orientation=horizontal)
axHistx.set_xlim( axScatter.get_xlim() )
axHisty.set_ylim( axScatter.get_ylim() )
plt.show()
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 23 / 24
-
Gracias
Gracias
Preguntas?
Mas preguntas?
M. Cardenas (CIEMAT) Graficas 22-26 de Abril de 2013 24 / 24
Table of ContentsObjectivosIntroduccinUna Grfica BsicaHistogramaBoxplotMltiples grficasAumentando la Complejidad