Tu primer gráfico
Una tabla de cien números no dice nada de un vistazo. Un gráfico, sí. Vamos a hacer uno con lo que ya sabes.
Your first chart
A table of a hundred numbers says nothing at a glance. A chart does. Let us build one with what you already know.
La idea detrás de una barra de un gráfico es simple: más cantidad, más caracteres
repetidos. Y repetir algo un número de veces ya lo sabes hacer: es un bucle
for, o el atajo "■" * n que multiplica un texto.
The idea behind a chart bar is simple: more quantity, more repeated
characters. And repeating something a number of times is something you already
know: a for loop, or the shortcut "■" * n which multiplies text.
Si un valor fuera 5.000, imprimir 5.000 símbolos sería un desastre. Hay que escalar: dividir cada valor por el más grande, para que la barra más larga mida siempre lo mismo, no importa la magnitud real de los datos.
If a value were 5,000, printing 5,000 symbols would be a mess. You need to scale: divide each value by the largest one, so the longest bar always measures the same, no matter the real magnitude of the data.
Esa cuenta —valor / máximo— se llama
normalizar: convertir cualquier escala a un rango fijo, en este caso de 0 a 1.
Es una idea que vas a encontrar en casi cualquier lugar donde se procesen datos.
That calculation — value / maximum — is called
normalising: converting any scale into a fixed range, here 0 to 1. An idea
you will run into almost anywhere data gets processed.
Con la misma idea de normalizar, calcular porcentajes es directo: cada valor dividido por el total —no el máximo— y multiplicado por 100.
With the same normalising idea, calculating percentages is direct: each value divided by the total — not the maximum — and multiplied by 100.
Un gráfico de texto es perfecto para revisar datos rápido, en una terminal o acá mismo.
Pero si necesitas gráficos de verdad —con colores, ejes, y que se puedan guardar como
imagen— existe matplotlib, la librería más usada de Python para esto. Ya la
mencionamos en el tutorial de módulos: se instala con pip install matplotlib y
se importa igual que cualquier otra.
No la cargamos acá porque es pesada y tardaría un buen rato en tu navegador —en tu computador, en cambio, es instantánea—. La idea de este tutorial era que entendieras qué hace un gráfico antes de usar una herramienta que lo hace por ti.
A text chart is perfect for checking data quickly, in a terminal or right here. But if you
need real graphics — with colours, axes, and something you can save as an image — there is
matplotlib, Python's most used library for this. We already mentioned it in
the modules tutorial: install with pip install matplotlib and import it like
any other.
We do not load it here because it is heavy and would take a while in your browser — on your computer, though, it is instant. The point of this tutorial was for you to understand what a chart does before using a tool that does it for you.
Convertir números en barras repitiendo caracteres, escalar valores para que quepan, normalizar, calcular porcentajes, y cuándo dar el salto a una librería como matplotlib.
Con esto cierras el curso de Python. Las cuatro bases, listas y diccionarios, texto, módulos, errores, comprensiones, clases, archivos, automatización y gráficos: ya tienes todo lo necesario para empezar un proyecto de verdad.
Turning numbers into bars by repeating characters, scaling values so they fit, normalising, calculating percentages, and when to reach for a library like matplotlib.
This closes the Python course. The four foundations, lists and dictionaries, text, modules, errors, comprehensions, classes, files, automation and charts: you now have everything you need to start a real project.