Matplotlib
Pyplot
#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
modify charts using pyplot
chart color
#do it with magenta color
plt.plot(x, y, "m")
plt.show()
Format strings
#do it with circles instead of dots
plt.plot(x, y, "o")
plt.show()
Line Style
#do it with dash-dot line style
plt.plot(x, y, "-.")
plt.show()
plt.plot(x, y, "gv:")
plt.show()
Labels, legends, title & size
axis labels & 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()
yticks
plt.plot(x, y)
plt.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()
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()
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
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()
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)
Modify bars width & bar colors
plt.bar(countrys,population, width=0.5, color= ["aqua", "grey", "teal", "crimson", "violet"])
plt.show()
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()
Horizontal bar plot
plt.barh(countrys,population)
plt.show()
Histogram
#remember our cuadratic function
print(x)
print(y)
plt.hist(y, bins = 10) #you can specify the number of bins
plt.show()
plt.hist(y, bins = 10, histtype="step") #you can specify the type of histogram
plt.show()
Pie chart
#remember our cuadratic function
print(x)
print(y)
plt.pie(y)
plt.show
Scatter plot
#correlation among x & y
plt.scatter(x,y)
plt.show()
customize scatter plot
#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()
Boxplot
#creating random data for boxplot
data = np.random.randint(0, 50, 100)
plt.boxplot(data)
plt.show()
change direction, fill interquartilic range & focus median
plt.boxplot(data, vert=False, patch_artist=True, notch=True)
plt.show()
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()
#with the argument showfliers you can decide if keep or remove outliers
plt.boxplot(data, showfliers=False)
plt.show()
Subplot
#Generating the data
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,5,11)
y = x ** 2
#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()
#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()
Multiple charts with the object-oriented method
Differences between pyplot & object-oriented
Creating charts with object-oriented method
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()
Subplots
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()
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")
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
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
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
Size
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)
Colors & Styles
import matplotlib.pyplot as plt
print( plt.style.available )
#grid style
plt.style.use('dark_background')
plt.plot(x, y)
plt.show()
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')
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')
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)
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')
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")