Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad

Parte 3 curso de python en estadistica, Ejercicios de Metodología de Investigación

Parte 3 curso de python en estadistica

Tipo: Ejercicios

2019/2020

Subido el 27/09/2021

hi-im-adc
hi-im-adc 🇨🇴

4 documentos

1 / 17

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
In [47]:
"Visualización con matplotlib"
In [48]:
"Lo primero es la activacion del modo inline - de esta manera las figuras aparecerán automát
icamente incrustradas en el notebook"
In [49]:
%matplotlib inline
In [50]:
importimport numpynumpy asas npnp
importimport matplotlib.pyplotmatplotlib.pyplot asas pltplt
In [51]:
fromfrom IPython.displayIPython.display importimport HTML
HTML('<iframe src="http://matplotlib.org/gallery.html#pylab_examples" width="800" height="60
0"></iframe>')
Out[47]:
'Visualización con matplotlib'
Out[48]:
'Lo primero es la activacion del modo inline - de esta manera las figuras aparecerán automát
icamente incrustradas en el notebook'
D:\Users\Sandra\anaconda3\lib\site-packages\IPython\core\display.py:717: UserWarning: Consid
er using IPython.display.IFrame instead
warnings.warn("Consider using IPython.display.IFrame instead")
Out[51]:
indexmodules |home | examples | gallery | pyplot | docs »
Depsy
Depsy 100th percentile
100th percentile
Travis-CI:
build
build passing
passing
Related TopicsRelated Topics
Documentation overview
Click on any image to see full size image and source codeClick on any image to see full size image and source code
Gallery
Lines, bars, and markers
Shapes and collections
Statistical plots
Images, contours, and fields
Pie and polar charts
Color
Text, labels, and annotations
Ticks and spines
Axis scales
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Vista previa parcial del texto

¡Descarga Parte 3 curso de python en estadistica y más Ejercicios en PDF de Metodología de Investigación solo en Docsity!

In [47]: "Visualización con matplotlib" In [48]: "Lo primero es la activacion del modo inline - de esta manera las figuras aparecerán automát icamente incrustradas en el notebook" In [49]: %matplotlib inline In [50]: import import numpynumpy asas npnp import import matplotlib.pyplotmatplotlib.pyplot asas pltplt In [51]: from from IPython.displayIPython.display importimport HTML HTML('') Out[47]: 'Visualización con matplotlib' Out[48]: 'Lo primero es la activacion del modo inline - de esta manera las figuras aparecerán automát icamente incrustradas en el notebook' D:\Users\Sandra\anaconda3\lib\site-packages\IPython\core\display.py:717: UserWarning: Consid er using IPython.display.IFrame instead warnings.warn("Consider using IPython.display.IFrame instead") Out[51]: home | examples | gallery | pyplot | docs » modules | index DepsyDepsy^ 100th percentile100th percentile Travis-CI: buildbuild^ passingpassing Related Topics Related Topics Documentation overview

Click on any image to see full size image and source codeClick on any image to see full size image and source code

Gallery Lines, bars, and markers Shapes and collections Statistical plots Images, contours, and fields Pie and polar charts Color Text, labels, and annotations Ticks and spines

In [52]: "La biblioteca matplotlib es gigantesca y es difícil hacerse una idea global de todas sus po sibilidades en una primera toma de contacto. Es recomendable tener a mano la documentación y la galería:" In [53]: HTML('') In [54]: Axis scales Subplots, axes, and figures Style sheets Specialty plots Showcase API pylab examples mplot3d toolkit Out[52]: 'La biblioteca matplotlib es gigantesca y es difícil hacerse una idea global de todas sus po sibilidades en una primera toma de contacto. Es recomendable tener a mano la documentación y la galería:' Out[53]: home | examples | gallery | pyplot | docs » Matplotlib Examples » previous | next | modules | index pylab_examples Examples » DepsyDepsy^ 100th percentile100th percentile Travis-CI: buildbuild^ passingpassing Related Topics Related Topics Documentation overview Matplotlib Examples pylab_examples Examples Previous: pylab_examples example code: animation_demo.py Next: pylab_examples example code: annotation_demo2.py This Page This Page Show Source

pylab_examples example code:pylab_examples example code:

annotation_demo.pyannotation_demo.py

(Source code)

In [61]: plt.plot(x, f(x), label="Función f(x)") plt.xlabel("Eje $x$") plt.ylabel("$f(x)$") plt.legend() plt.title("Función $f(x)$") In [62]: "Personalización" In [63]: "La funcion plot acepta una serie de argumentos de personalizacion el aspecto de la función" In [64]: plt.plot(x, f(x), 'ro') plt.plot(x, 1 - f(x), 'g--') Out[61]: Text(0.5, 1.0, 'Función $f(x)$') Out[62]: 'Personalización' Out[63]: 'La funcion plot acepta una serie de argumentos de personalizacion el aspecto de la función' Out[64]: [<matplotlib.lines.Line2D at 0x18552976730>]

In [65]: plt.plot(x, f(x), color='red', linestyle='', marker='o') plt.plot(x, 1 - f(x), c='g', ls='--') In [66]: N = 100 x = np.random.randn(N) y = np.random.randn(N) plt.scatter(x, y) In [67]: "La función scatter muestra una nube de puntos, con posibilidad de variar también el tamaño y el color." In [68]: "Con s y c podemos modificar el tamaño y el color respectivamente. Para el color, a cada val or numérico se le asigna un color a través de un mapa de colores; ese mapa se puede cambiar con el argumento cmap. Esa correspondencia se puede visualizar llamando a la función colorba Out[65]: [<matplotlib.lines.Line2D at 0x18552d50460>] Out[66]: <matplotlib.collections.PathCollection at 0x18552da2eb0> Out[67]: 'La función scatter muestra una nube de puntos, con posibilidad de variar también el tamaño y el color.'

returnreturn np.cos(x) + np.sin(y) ** 2 In [72]: x = np.linspace(- 2 , 2 ) y = np.linspace(- 2 , 2 ) xx, yy = np.meshgrid(x, y) plt.contour(xx, yy, f(xx, yy)) plt.colorbar() In [73]: "La función contour se utiliza para visualizar las curvas de nivel de funciones de dos varia bles y está muy ligada a la función np.meshgrid" In [74]: zz = f(xx, yy) plt.contourf(xx, yy, zz, np.linspace(-0.5, 2.0)) plt.colorbar() In [75]: Out[72]: <matplotlib.colorbar.Colorbar at 0x18552f74eb0> Out[73]: 'La función contour se utiliza para visualizar las curvas de nivel de funciones de dos varia bles y está muy ligada a la función np.meshgrid' Out[74]: <matplotlib.colorbar.Colorbar at 0x185530b12b0>

"Para guardar las gráficas en archivos aparte podemos usar la función plt.savefig." In [76]: "Interfaz orientada a objetos" In [77]: def def f(x): return return np.exp(-x ** 2 ) In [78]: x = np.linspace(- 1 , 5 , num= 30 ) In [79]: x In [80]: f(x) In [81]: fig, axes = plt.subplots() axes.plot(x, f(x), 'ro', label="Función F(x)") axes.set_xlim(- 2 , 4 ) axes.set_ylim(- 1 , 2 ) fig.savefig("grafica1.png") Out[75]: 'Para guardar las gráficas en archivos aparte podemos usar la función plt.savefig.' Out[76]: 'Interfaz orientada a objetos' Out[79]: array([-1. , -0.79310345, -0.5862069 , -0.37931034, -0.17241379, 0.03448276, 0.24137931, 0.44827586, 0.65517241, 0.86206897, 1.06896552, 1.27586207, 1.48275862, 1.68965517, 1.89655172, 2.10344828, 2.31034483, 2.51724138, 2.72413793, 2.93103448, 3.13793103, 3.34482759, 3.55172414, 3.75862069, 3.96551724, 4.17241379, 4.37931034, 4.5862069 , 4.79310345, 5. ]) Out[80]: array([3.67879441e-01, 5.33117686e-01, 7.09185234e-01, 8.65994832e-01, 9.70710971e-01, 9.98811646e-01, 9.43400884e-01, 8.17952307e-01, 6.50996579e-01, 4.75607235e-01, 3.18960733e-01, 1.96356377e-01, 1.10961373e-01, 5.75595853e-02, 2.74083260e-02, 1.19802640e-02, 4.80694649e-03, 1.77048075e-03, 5.98593712e-04, 1.85777072e-04, 5.29262471e-05, 1.38410522e-05, 3.32266179e-06, 7.32187295e-07, 1.48107572e-07, 2.75011817e-08, 4.68753445e-09, 7.33426522e-10, 1.05338655e-10, 1.38879439e-11])

In [84]: x = np.linspace(- 2 , 2 ) y = np.linspace(- 2 , 2 ) xx, yy = np.meshgrid(x, y) In [85]: def def g(x, y): return return np.cos(x) + np.sin(y) ** 2 In [86]: zz = g(xx, yy) In [87]: fig, axes = plt.subplots() axes.contourf(xx, yy, zz, np.linspace(- 1 , 1 ), cmap=plt.cm.autumn) In [88]: "Ejemplo con datos reales" In [89]: !more temperaturas.csv Out[87]: <matplotlib.contour.QuadContourSet at 0x185531f5c40> Out[88]: 'Ejemplo con datos reales' STATION,DATE,TMAX,TMIN GHCND:USW00094728,20130101,44,- GHCND:USW00094728,20130102,6,- GHCND:USW00094728,20130103,0,- GHCND:USW00094728,20130104,28,- GHCND:USW00094728,20130105,56, GHCND:USW00094728,20130106,78, GHCND:USW00094728,20130107,72, GHCND:USW00094728,20130108,89, GHCND:USW00094728,20130109,94, GHCND:USW00094728,20130110,83,

In [90]: datos = np.loadtxt("temperaturas.csv", usecols=( 1 , 2 , 3 ), skiprows= 1 , delimiter=',') In [91]: fig, axes = plt.subplots() x = np.arange(len(datos[:, 1 ])) temp_media = (datos[:, 1 ] + datos[:, 2 ]) / 2 axes.plot(x, datos[:, 1 ], 'r') axes.plot(x, datos[:, 2 ], 'b') axes.plot(x, temp_media, 'k') In [ ]: In [ ]: In [ ]:

GHCND:USW00094728,20131214,11,-

GHCND:USW00094728,20131215,44,-

GHCND:USW00094728,20131216,6,-

GHCND:USW00094728,20131217,0,-

GHCND:USW00094728,20131218,28,-

GHCND:USW00094728,20131219,83,-

GHCND:USW00094728,20131220,117,

GHCND:USW00094728,20131221,183,

GHCND:USW00094728,20131222,217,

GHCND:USW00094728,20131223,178,

GHCND:USW00094728,20131224,56,-

GHCND:USW00094728,20131225,-5,-

GHCND:USW00094728,20131226,22,-

GHCND:USW00094728,20131227,44,-

GHCND:USW00094728,20131228,128,

GHCND:USW00094728,20131229,89,

GHCND:USW00094728,20131230,72,-

GHCND:USW00094728,20131231,0,-

GHCND:USW00094728,20140101,6,-

Out[91]: [<matplotlib.lines.Line2D at 0x18554301040>]

In [ ]: In [ ]: