|
14 | 14 | __email__ = "alumnos@inove.com.ar"
|
15 | 15 | __version__ = "1.1"
|
16 | 16 |
|
| 17 | +import csv |
17 | 18 | import matplotlib.pyplot as plt
|
18 | 19 |
|
19 | 20 |
|
20 | 21 | 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() |
22 | 36 |
|
23 | 37 |
|
24 | 38 | 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() |
26 | 65 |
|
27 | 66 |
|
28 | 67 | 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() |
30 | 91 |
|
31 | 92 | if __name__ == '__main__':
|
32 | 93 | print("Bienvenidos a otra clase de Inove con Python")
|
|
0 commit comments