Python Parte Grafica
ING1310 – Introducci´n a la Computaci´n o o
Python
Graficando Mat´ Recabarren - Andr´s Moreno ıas e mrecabarren@uandes.cl - andres@moreno.cl
Universidad de Los Andes
27/octubre/2010
Rev : 110
Clase 24 1 / 23
Python
En esta clase
1 matplotlib
M´ltiples Figuras u Informaci´n Adicional o
2 Ejercicios
Clase 24
2 / 23
Python matplotlib
matplotlibClase 24
3 / 23
Python matplotlib
Introducci´n o
• matplotlib es un m´dulo que agrega funcionalidades para graficar o
en python
• El m´dulo en python se llama pyplot o
• Crea una ventana por cada gr´fico que realicemos a • Cada ventana se conoce como figure • Existe un m´todo show el cual se debe llamar una sola vez, cuando ya e
est´n listos todos los gr´ficos. a a
• Este m´todomuestra las ventanas. e
Clase 24
4 / 23
Python matplotlib
Funci´n plot o
• Es la funci´n b´sica para graficar. o a • Su funcionamiento depender´ de los par´metros que se le entreguen: a a • plot(y) → el arreglo recibido lo considera como el eje Y, el eje X es el ´ ındice en el arreglo. • plot(x, y) → grafica linealmente entre los puntos x, y obtenidos de los dos arreglos. • plot(x, y,f) → el par´metro f indica el formato del gr´fico, a a definido por el color y el estilo:
• • • • • ’b-’ → valor por defecto, azul y l´ ınea entre los puntos (continuo) ’g–’ → verde y l´ ınea punteada entre los puntos ’ro’ → rojo y un c´ ırculo en cada punto ’ks’ → negro y cuadrado en cada punto ’mˆ → morado y tri´ngulo en cada punto ’ a
• Adem´s se pueden combinar los estilos, i.e. ’go–’ aClase 24
5 / 23
Python matplotlib
from numpy import * import matplotlib . pyplot as plt y = array ([1 , 2 , -1 , 5 , 2 , 0 , 3]) x = array ([ -1 , 0.5 , 3 , 5 , 6 , 6.5 , 6.9]) plt . plot (x , y , ’ro - - ’) plt . show ()
Clase 24
6 / 23
Python matplotlib
• Por cada llamada a plot se agrega un nuevo conjunto al mismo
gr´fico a
Ejemplo: M´ltiples resultados en un mismogr´fico u a from numpy import * import matplotlib . pyplot as plt t = linspace (0 , 10 , 40) y = sin ( t ) plt . plot (t , y ) # graficar seno plt . plot (t , cos ( t ) ) # graficar coseno plt . show () # mostrar
Clase 24
7 / 23
Python matplotlib
Configuraci´n de Ejes o
• Es posible modificar la configuraci´n de los ejes o • axis([xmin, xmax, ymin, ymax]) → modifica los valores m´ximos a y m´ınimos de cada eje. • xlabel/ylabel(l) → recibe un nombre con el t´ ıtulo para el eje. • title(t) → define el t´ ıtulo del gr´fico. a • legend() → agrega una leyenda al gr´fico (se debe poner un label al a gr´fico) a
Clase 24
8 / 23
Python matplotlib
Ejemplo: Gr´fico con configuraci´n de ejes a o from numpy import * import matplotlib . pyplot as plt t = linspace (0 , 10 , 40) y = sin ( t )plt . plot (t , y , label = " sin ( t ) " ) # graficar seno plt . axis ([ -1 , 11 , -1.5 , 1.5]) plt . xlabel ( ’ tiempo ’) plt . ylabel ( ’ seno ’) plt . title ( ’ Seno en el tiempo ’) plt . legend () plt . show ()
Clase 24
9 / 23
Python matplotlib
Histogramas
• matplotlib provee una funci´n especial para graficar histogramas. o • La funci´n hist recibe al menos el arreglo con losvalores y la o
cantidad de bins
• Los bins son las agrupaciones para el histograma.
Ejemplo: Mostrar histogramas from numpy import * import matplotlib . pyplot as plt # Genera un arreglo de 1000 valores aleatorios con # distribucion normal de media 0 y phi 5 y = random . normal (0 , 5 , 1000) plt . hist (y , 20) plt . plot ([ -20 ,20] ,[ len ( y ) /20 , len ( y ) /20] , ’r - ’) plt . show ()Clase 24 10 / 23
Python matplotlib M´ltiples Figuras u
matplotlib
M´ltiples Figuras u
Clase 24
11 / 23
Python matplotlib M´ltiples Figuras u
¿Qu´ pasa si quiero hacer dos gr´ficos en un mismo e a programa?
• Por defecto se crea una figura donde se dibujan todos los gr´ficos a • Para gr´ficos separados se deben crear las figuras de forma expl´ a ıcita • A trav´s de la...
Regístrate para leer el documento completo.