Matplotlib#

What charts should I use?#

Picture title

https://datavizproject.com/

Pyplot#

pyplot is the tool from matplotlib that will allow you run charts in a simple way.

(this tool allows you to see one chart at a time)

For multiple charts, refer to subplot

#import import matplotlib.pyplot as plt

import matplotlib.pyplot as plt
import numpy as np

#create a cuadratic function

#set the domain of the function

x = np.linspace(0, 5, 11)

y = x**2

#creating the chart

plt.plot(x, y)

plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
../_images/4188232f018c966940e19ed52e8cdc157811637d3a7801e9b37e95b326244ec3.png

modify charts using pyplot#

chart color#

Picture title

#do it with magenta color

plt.plot(x, y, "m")
plt.show()
../_images/1ac3b302fbb026b324662e54e5bdd2f88ef8f76c54ad40561f8bcd34ede4308c.png

Format strings#

Picture title

#do it with circles instead of dots

plt.plot(x, y, "o")
plt.show()
../_images/379e27a24a807bf7475d7de1d1f9327ab6bbf5dc3f914a3fee3465bc39bb4981.png

Line Style#

Picture title

#do it with dash-dot line style

plt.plot(x, y, "-.")
plt.show()
../_images/dbd47d13635f236451ee99e684bb5daf97b6a5f4680083b4be284375537f9e9b.png

Green colored chart, with triangle_down marker & dotted line style

plt.plot(x, y, "gv:")
plt.show()
../_images/dcf6469a662a0273a5a27598f94166ccf670495460805f6d3be56d5b9bc7ea71.png

Labels, legends, title & size#

axis labels & title#

use

plt.xlabel(” name_of_the_x_axis “) for the xlabel

plt.ylabel(” name_of_the_y_axis “) for the ylabel

plt.title(” title “) for the title

#name the x axis as "Domain" & the y axis as "Range"
#put a title "Cuadratic function"

plt.plot(x, y)
plt.xlabel("Domain")
plt.ylabel("Range")
plt.title("Cuadratic function")
plt.show()
../_images/39df15f4eb2adef5afea378d278228fa9263d7d2fd3d1c2705f35e28f3ede8a2.png

yticks#

You can modify the ticks (blue marked y axis numbers in the picture below:)

Picture title

example:

note this chart from the previous leeson

plt.plot(x, y)
plt.show()
../_images/4188232f018c966940e19ed52e8cdc157811637d3a7801e9b37e95b326244ec3.png

for some reason, i want the negative y axis to show

plt.plot(x, y)

#create a list with the yticks that you want to show
plt.yticks(np.linspace(-25, 25, 11))
print("The list of y ticks created with linspace is", np.linspace(-25, 25, 11))

#show the chart
plt.show()
The list of y ticks created with linspace is [-25. -20. -15. -10.  -5.   0.   5.  10.  15.  20.  25.]
../_images/29a08aed7c82fe2183f1b6bbdfcdeb181df4c039bb9acf0dbe45dc423a95be84.png

you can even put a name to each tick

plt.plot(x, y)

#create a list with the yticks that you want to show
list_yticks = [-25, -15, -5, 0, 5, 15, 25]

#now create a list with the names for each ytick
namesfor_yticks = ["-25B","-15B","-5B","0","5B","15B","25B"] 
###this is supossing you want to show the numbers in billions


#add the yticks
plt.yticks(list_yticks, namesfor_yticks)

#show the chart
plt.show()
../_images/cd9f765daa86b4b21fe186a068fa590998c9e73cb8f9bc1c140408562a5b4486.png

Different chart types#

#The charts will have the following data

x = np.linspace(0, 5, 11)

y = x**2

y_2 = x**3

Line chart#

plt.plot(x,y)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
../_images/4188232f018c966940e19ed52e8cdc157811637d3a7801e9b37e95b326244ec3.png

Multiple line chart#

#to do a multiple line chart just put the plots one after the otherabs

#cuadratic function
plt.plot(x,y)
#cubic function
plt.plot(x,y_2)

#show the plot
plt.show()
../_images/1ca346f142b72aee2a4ea815bd0ac24c4d9e330ef35550c2b1f35b5e98dbb5b0.png

Bar plot#

import matplotlib.pyplot as plt
import numpy as np

#categorical variables
countrys = ["INDIA", "JAPAN", "MEXICO", "COLOMBIA", 'GERMANY']
population = [5000, 800, 900, 1000, 300]

#bar plot
plt.bar(countrys, population)
<BarContainer object of 5 artists>
../_images/87ce6a09e3c87b1659c86fec630355c94af7433d9a86131c16284df7b9b8e0b4.png

Modify bars width & bar colors#

plt.bar(countrys,population,  width=0.5, color= ["aqua", "grey", "teal", "crimson", "violet"])
plt.show()
../_images/a58f6f386e84f8615f5036312f086df26a3f19f90dd30f1e96aa7cd2315ec483.png

xticks & xlabel rotation#

plt.bar(countrys,population, width=0.5, color= ["aqua", "grey", "teal", "crimson", "violet"])
plt.xticks(np.arange(5), ('India','Japon', 'Mexico', 'Colombia', 'Alemania'), rotation = 45)
plt.show()
../_images/343d52aaeac9c87aa430dee36c4d0c6caccf186ff30f9eb1b5edc9b01ba25af4.png

Horizontal bar plot#

plt.barh(countrys,population)
plt.show()
../_images/ea0fc47ef2c9ff6a9f4d8d984708f0cddc8e48711bca1f4191c0b53a8595991a.png

Histogram#

#remember our cuadratic function

print(x)
print(y)
[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
[ 0.    0.25  1.    2.25  4.    6.25  9.   12.25 16.   20.25 25.  ]

let’s create a histogram of the y values

plt.hist(y, bins = 10)    #you can specify the number of bins
plt.show()
../_images/a4b45c8c76b2871b958c57fbe1938d071a6debe5fcf2061ab95eea7a629ab57b.png
plt.hist(y, bins = 10, histtype="step")    #you can specify the type of histogram
plt.show()
../_images/45f941853cf71d941af35ecdb49e8e089319369f69e31cb5fa1978eb66ed83a4.png

Pie chart#

#remember our cuadratic function

print(x)
print(y)
[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
[ 0.    0.25  1.    2.25  4.    6.25  9.   12.25 16.   20.25 25.  ]

Let’s print the pie chart

plt.pie(y)

plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
../_images/cdcf652ed0618c64aceb0b01c686487cd1b6a1ce311c9a182e133c5afbf00a61.png

Scatter plot#

This one is used to know correlation between variables

#correlation among x & y
plt.scatter(x,y)

plt.show()
../_images/379e27a24a807bf7475d7de1d1f9327ab6bbf5dc3f914a3fee3465bc39bb4981.png

customize scatter plot#

in a scatter plot, you can change:

  • bubble size —> s

  • bubble shape(marker)—> marker

  • bubble color —> c

  • bubble transparency —> alpha

#let's create the data
xscatter = np.random.rand(50)
yscatter = np.random.rand(50)

#i want the bubble size to be random
area = (30 * np.random.rand(50)) **2

#i also want random colors
colors = np.random.rand(50)

plt.scatter(xscatter,yscatter, s=area, c= colors, marker = 'o', alpha= 0.5)
plt.show()
../_images/613e0d3a202b8dbf6f8703e2ecc3a5730077f404013ace3fa904227a333ecbe1.png

Boxplot#

#creating random data for boxplot
data = np.random.randint(0, 50, 100)
plt.boxplot(data)
plt.show()
../_images/9c63bdc342dfba0738a159a7814a995f39771c90b0f506049a5de8b9fe26ae13.png

change direction, fill interquartilic range & focus median#

change direction:

  • vert = False fill interquartilic range:

  • patch_artist=True Focus median:

  • notch = True

plt.boxplot(data, vert=False, patch_artist=True, notch=True)
plt.show()
../_images/98aba87ba4c297fc465b0c060c47000962d3477da8207cbf9f616b2e882b46f8.png

Remove outliers (datos atipicos)#

#remember that data are numbers from 1 to 50. let's append a 200
data = np.append(data, 200)

plt.boxplot(data)
plt.show()
../_images/f0e838d5c7526dd1b5986104bfa045b88e368a2eb9c1dd085f2601c70b13cce6.png

that outlier is a problem, remove it so we can see the chart in a better way

#with the argument showfliers you can decide if keep or remove outliers
plt.boxplot(data, showfliers=False)
plt.show()
../_images/5471eec7fcb640b8ee5df77126afe68d3ebb669a6a3e82583571b2a09c69984d.png

Subplot#

Subplot creates a matrix of charts

Picture title

#Generating the data

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,5,11)
y = x ** 2

Now let’s create the matrix of charts

the matrix will be 1x2

#Chart number 1
plt.subplot(1, 2, 1)                                #1 row, 2cols, 1st index
plt.plot(x, y, "r--")

#chart number 2
plt.subplot(1, 2, 2)                                #1 row, 2cols, 2nd index
plt.pie(y)

#show the plot
plt.show()
../_images/6547a8e7917eda70c1667ab01782c633ad59dc4ced76c670f630be903e766fa9.png

now let’s create a 2x2 matrix

#let's create another variable
twotimesx = x*2

#Chart number 1
plt.subplot(2, 2, 1)                                #1 row, 2cols, 1st index
plt.plot(x, y, "r--")
plt.plot(x, twotimesx, "b--")

#chart number 2
plt.subplot(2, 2, 2)                                #1 row, 2cols, 2nd index
plt.pie(y)

#chart number 3
plt.subplot(2, 2, 3)                                #1 row, 2cols, 2nd index
plt.hist(y)

#show the plot
plt.show()
../_images/fd98aa0a31b9992ac94eba24e5acf11866b365f0580b25a0835e7ad0f7e85508.png

Multiple charts with the object-oriented method#

in this method, an object defines a figure, that figure is a canva that contains multiple charts called axes

  • the figure is a canva

  • inside of the figure, charts are axes

  • inside of the axes, each chart has teir own axis

Picture title

Differences between pyplot & object-oriented#

Picture title

Creating charts with object-oriented method#

Picture title

import matplotlib.pyplot as plt
import numpy as np

#creating the data
x = np.linspace(0, 5, 11)
y = x**2              #cuadratic function
y_2 = x*2             #just x two times


#let's create the figure
fig = plt.figure()

#let's add the axes
axes = fig.add_axes([0.1, 0.1, 0.5, 0.9])         #arguments -> (xposition,yposition,width, height)
axes2 = fig.add_axes([0.1, 0.6, 0.4, 0.3])        #arguments -> (xposition,yposition,width, height)
#notice how you can customize the chart with those arguments


axes.plot(x,y, "r")
axes2.pie(y_2)


fig.show()
C:\Users\admin\AppData\Local\Temp\ipykernel_18244\1285430673.py:23: UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backend, so cannot show the figure.
  fig.show()
../_images/e7e8d2c2964758283ba0209e4304419307cf3592a2ec896047e130753d7dca56.png

With this object oriented method, you can personalize

Picture title

Subplots#

Does the same as subplot, but the difference is that this one gets all the advantages of the object oriented method from matplotlib

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 11)

y = np.sin(x)

fig, axes = plt.subplots()
axes.plot(x, y)

plt.show()
../_images/e2ea0f4d94e70f8fe97e524557405a000329a80c2fc901ea8d25651e43545dfa.png

multiple charts#

#this creates a figure with two charts
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].plot(x,y,"b")
axes[1].plot(y,x,"r")
[<matplotlib.lines.Line2D at 0x2297f5f9650>]
../_images/a63ca5fc9b0fe4219bf7932bac28120e359f6a9e3237273c281bce24c1f5fed9.png

Matrix of charts#

You can use the object oriented method or fig type to create a matrix of charts

matrix of charts with fig (call by index)#

fig, axes = plt.subplots(nrows=2,ncols=4)

#we will access the charts through the chart index in the matrix
axes[0,0].plot(x,y)
axes[0,1].plot(y,x, 'r')
axes[0,2].hist(y)

fig.tight_layout() #mejora la visualización de los ejes de cada gráfico
../_images/968797cbc32ea64ea7cf10e27fdeef4a62a26d201e36aeafdab6d3b3d23d9ca7.png

matrix of charts with fig (call by name)#

#same functionality as above but now we won't call by positions but by labels

fig, ((sinx, cosx, tanx, sin2x), (axes5, axes6, axes7, axes8)) = plt.subplots(nrows=2,ncols=4)

#genera un trazo accediendo a las graficas a traves del indice de la matriz
sinx.plot(x, np.sin(x), "b")
cosx.plot(x, np.cos(x), "r")
tanx.plot(x, np.tan(x), "g")
sin2x.plot(x, np.sin(2*x), "b")

fig.tight_layout() #mejora la visualización de los ejes de cada gráfico
../_images/664ae948744da612bbb42c78c6e4f06510bb22e205e3bc97ad89f320d4fda9e7.png

Labels, Legends, title & size (for fig objects)#

Titles , Labels & Legends#

fig, (ax1,ax2) = plt.subplots(1,2)

#first plot
ax1.plot(x,y, label="sin(x)")     #label for first plot & plot definition
ax1.set_title("X - Y Relation")     #title for first plot
ax1.set_xlabel("X")     #xlabel for first plot
ax1.set_ylabel("Y")     #ylabel for first plot
ax1.legend()            #legend for first plot

#second plot
ax2.plot(y,x, label="cos(x)")     #label for second plot & plot definition
ax2.set_title("Y - X Relation")  #title for second plot
ax2.set_xlabel("Y")    #xlabel for second plot
ax2.set_ylabel("X")    #ylabel for second plot
ax2.legend()           #legend for second plot
<matplotlib.legend.Legend at 0x2297f234390>
../_images/e4e1683071d85c8c60f39f807f3b7a3e82224dcc206bbe6a95b168d4305f33f9.png

Size#

you can modify the size with the argument figsize, which receives a tuple (width, length)

fig, (ax1,ax2) = plt.subplots(1,2, figsize= (4,6))

ax1.plot(x, y)
ax2.plot(x,y)

fig, (ax1,ax2) = plt.subplots(1,2, figsize= (1,3))

ax1.plot(x, y)
ax2.plot(x,y)
[<matplotlib.lines.Line2D at 0x2297df01890>]
../_images/2956dc5f47f455d14035759b80e6e57cde8aee97133d8c3b7a88a6221c5bda55.png ../_images/d5b59e2578641d3dd0fb65f6e7f5c146846dc9689a3f54407b41af05a2338f2d.png

Colors & Styles#

import matplotlib.pyplot as plt

print( plt.style.available )
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']

the list above is showing all the available styles, let’s select “dark_background” to test

#grid style

plt.style.use('dark_background')

plt.plot(x, y)
plt.show()
../_images/3493e7b89d0355f0057fd38c9f9ea874cb00e2a6e44d60b308a3fcf03f4a0bfa.png
plt.style.use("seaborn-whitegrid")

fig, ax = plt.subplots()
ax.plot(x,x+1, 'r--')
ax.plot(x,x+2, 'bo-')
ax.plot(x,x+3, 'g.:')
ax.plot(x,x+4, 'purple')
C:\Users\admin\AppData\Local\Temp\ipykernel_18244\1747373470.py:1: MatplotlibDeprecationWarning: The seaborn styles shipped by Matplotlib are deprecated since 3.6, as they no longer correspond to the styles shipped by seaborn. However, they will remain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn API instead.
  plt.style.use("seaborn-whitegrid")
[<matplotlib.lines.Line2D at 0x2297fe4bd90>]
../_images/ad6745aedfcf74514decd4bad0a102d3c5d4e0f4366689149f68f86596220f88.png

Personalize colors & styles in pyplot#

color#

fig, ax = plt.subplots(figsize = (6,6))
ax.plot(x,x+1,color = '#D426C8')  #---> RGB Color
ax.plot(x,x+2,color = '#66FF89')
ax.plot(x,x+3,color = 'blue')     #---> Common Color
ax.plot(x,x+4, color = 'black')
[<matplotlib.lines.Line2D at 0x2297fe159d0>]
../_images/96ae4b70336dad9b88257896055be4453ed8370257b95b437c6c6fc1854a7ca5.png

transparency & line width#

fig, ax = plt.subplots(figsize=(6,6))

ax.plot(x,x+1, color="#D426C8", alpha=0.5, linewidth=18)
ax.plot(x,x+2,color = '#66FF89', linewidth= 3)
ax.plot(x,x+3,color = 'blue', linewidth= 5)
ax.plot(x,x+4, color = 'black', alpha = 0.3, linewidth= 12)
[<matplotlib.lines.Line2D at 0x2297fab1d90>]
../_images/53e874bc49dbff3324960c7824897c7b1084fbc48264f98ce3df4d3c84a7c448.png

linestyle & markers#

fig, ax = plt.subplots(figsize = (6,6))
ax.plot(x,x+3,color = 'blue', linestyle = 'dashed', marker = 'x')
ax.plot(x,x+4, color = 'black',linestyle = '--', marker = 'P')
[<matplotlib.lines.Line2D at 0x2297fe9a590>]
../_images/71587a300d901663082ebed1e9ee2e1f432c5db42761cbf58b07d19cadbcf7f2.png

marker size & marker face color#

fig, ax = plt.subplots(figsize = (6,6))
ax.plot(x,x+3,color="blue",linestyle="dashed",marker="8",markersize=10,markerfacecolor= "#37D842")
ax.plot(x,x+4, color = 'black',linestyle = '--', marker = 'P', markerfacecolor="#FF0000")
[<matplotlib.lines.Line2D at 0x229021a9d90>]
../_images/b6e0c27edb4470f8bfc17b17c120c53d4914eb1b195ef9487adb03a8490d3f20.png
Created in deepnote.com Created in Deepnote