Skip to content

Commit ca61874

Browse files
author
inoveAlumnos
committed
Update ejemplos de clase
1 parent ba54413 commit ca61874

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

ejemplos_clase.py

+64-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,80 @@
1414
__email__ = "alumnos@inove.com.ar"
1515
__version__ = "1.1"
1616

17+
import csv
1718
import matplotlib.pyplot as plt
1819

1920

2021
def line_plot():
21-
pass
22+
years = [1900, 1970, 1990, 2000, 2020]
23+
poblacion = [1650, 3692, 5263, 6070, 7800]
24+
25+
# Realizaremos un gráfico "plot" con:
26+
# years como "x"
27+
# poblacion como "y"
28+
fig = plt.figure()
29+
fig.suptitle('Población histórica mundial', fontsize=16)
30+
ax = fig.add_subplot()
31+
32+
ax.plot(years, poblacion, c='darkgreen')
33+
ax.legend()
34+
ax.grid()
35+
plt.show()
2236

2337

2438
def scatter_plot():
25-
pass
39+
years = []
40+
poblacion = []
41+
42+
# Los datos en el archivo poblacion
43+
# no estan ordenados por año
44+
with open('poblacion.csv') as fi:
45+
data = csv.DictReader(fi)
46+
for line in data:
47+
years.append(int(line['year']))
48+
poblacion.append(int(line['poblacion']))
49+
50+
fig = plt.figure()
51+
fig.suptitle('Población histórica mundial', fontsize=16)
52+
ax1 = fig.add_subplot(1, 2, 1)
53+
ax2 = fig.add_subplot(1, 2, 2)
54+
55+
# Como los datos no están ordenados por año
56+
# el plot no funcionará bien en este caso
57+
ax1.plot(years, poblacion, c='darkgreen')
58+
ax1.legend()
59+
ax1.grid()
60+
61+
ax2.scatter(years, poblacion, c='darkred')
62+
ax2.legend()
63+
ax2.grid()
64+
plt.show()
2665

2766

2867
def bar_plot():
29-
pass
68+
years = []
69+
poblacion = []
70+
objetivos = [2000, 2005, 2010, 2015, 2020]
71+
72+
# Los datos en el archivo poblacion
73+
# no estan ordenados por año
74+
with open('poblacion.csv') as fi:
75+
data = csv.DictReader(fi)
76+
for line in data:
77+
year = int(line['year'])
78+
# Filtramos los datos por un criterio de búsqueda
79+
if year in objetivos:
80+
years.append(year)
81+
poblacion.append(int(line['poblacion']))
82+
83+
fig = plt.figure()
84+
fig.suptitle('Población histórica mundial', fontsize=16)
85+
ax = fig.add_subplot()
86+
87+
ax.bar(years, poblacion)
88+
ax.legend()
89+
ax.grid()
90+
plt.show()
3091

3192
if __name__ == '__main__':
3293
print("Bienvenidos a otra clase de Inove con Python")

poblacion.csv

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
year,poblacion
2+
1800,978
3+
1850,1262
4+
1900,1650
5+
1000,310
6+
1750,791
7+
1950,2518
8+
1955,2755
9+
1960,2982
10+
1965,3334
11+
1970,3692
12+
2008,6709
13+
2010,6863
14+
2011,7082
15+
2017,7722
16+
2018,7750
17+
1975,4068
18+
1980,4434
19+
1985,4830
20+
1990,5263
21+
1995,5674
22+
2000,6070
23+
2005,6453
24+
2015,7508
25+
2019,7770
26+
2020,7800

0 commit comments

Comments
 (0)