Matplotlib
Matplotlib és una biblioteca que serveix per dibuixar figures, funcions i tota mena de gràfiques en imatges estàtiques.
Introducció
Matplotlib és una llibreria de baix nivell que serveix per dibuixar figures, funcions i tota mena de gràfiques estàtiques (en imatges png).
Funciona molt bé amb estructures de Python, arrays de NumPy , sèries i dataFrames de Polars, etc.
Tot i que hi ha llibreries estàtiques més modernes i senzilles a Python (Seaborn) i altres que ofereixen gràfics animats (Plotly, Bokeh) ens interessa aprendre com funciona Matplotlib, perquè és la que ofereix el màxim nivell de personalització i perquè totes aquestes llibreries tenen un funcionament similar a Matplotlib.
Per aquest motiu, encara hi ha molts treballs científics que presenten les funcions i gràfics amb Matplotlib.
Static plots
Although dashboards, and interactive plots, are an extremely important aspect in exploring and presenting data, there will always be a requirement for static plots.
Reports, technical papers, articles, and anything in print are always going to require static plots. Being able to quickly and easily produce clear, logical and beautiful static plots therefore remains absolutely essential.
Static plots require all the information to be displayed effectively in one view. There are no dynamic overlays, or zooming and panning, to help gain further information on the fly like you might see in a dashboard or interactive plot.
As the old saying goes “a picture can convey a thousand words”. However, the CAN in that statement is very important! If the plot is poorly constructed, the message definitely will not be conveyed effectively.
Background
Matplotlib has been around since 2003!
It is fairly ubiquitous in the field of data science, and it would be fair to say that a lot of people have a love-hate relationship with it. It is very flexible, but can also get quite involved.
A quick recommendation
One of the major reasons for confusion (or straight up hate!), at least in my opinion, is that Matplotlib actually has two different interfaces:
- the “pyplot” interface, which is intended to be simpler to use for quick plots
- the “Axes” interface, which is much more flexible, and uses an Object Oriented Programming (OOP) approach
The “Axes” interface is actually pretty easy to use, and much more flexible. I would therefore encourage anyone using Matplotlib to just learn “Axes” interface (also sometimes referred to as an API.
For more on the differences between the two interfaces (or APIs), please see here:
API Reference – Matplotlib 3.8.2 documentation
Entorn de treball
Crea un nou projecte amb uv:
Modifica el fitxer main.py:
=
Executa el fitxer:

Pots veure que quan s’executa plt.show() s’obre un panell amb el gràfic
Una altra opció és guardar el gràfic en un fitxer amb savefig:
=
Obre el fitxer plot.png:

Pots veure que s’ha generat un gràfic de la seqüència xs.
Crear gràfics de senzills amb el mètode plot()
El gràfic més senzill que podem crear és una línia a partir d’una matriu plana (1D) o una llista.
En importar, ja creem un contenidor (un canvas = un llenç) dins de l’objecte plt.
El mètode plot és el que dibuixa el gràfic dins del contenidor plt.
Si no li diem res més, crea un gràfic de punts amb les dades de la llista que li hem passat.
=
You're reading a preview.
Sign in to read the full article. Any account opens 4 free articles a month; students and teachers read their course pages without limit.
Sign in