Mastering Data Visualization with Matplotlib: A Comprehensive Tutorial

Mastering Data Visualization with Matplotlib: A Comprehensive Tutorial

"Unleashing the Power of Matplotlib: A Step-by-Step Guide to Data Visualization in Python"


Installing MatPlotLib:

pip install matplotlib

Matplotlib Plotting:

To plot x and y points in Matplotlib, you can use the plot function. To do so, you’ll need to identify the x and y values for each point, respectively. Let’s dive into an example below.

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()


Matplotlib Markers:

You can use the keyword argument marker to emphasize each point with a specified marker:

Types of markers available:

import numpy as np
import matplotlib.pyplot as plt
X = [1, 2, 3, 4]
Y = [1, 4, 9, 16]
plt.plot(X,Y,marker="o")
plt.show()


Matplotlib Types of Lines:

Dotted:

plt.plot(X,Y, linestyle = 'dotted')

Dashed:

plt.plot(X,Y, linestyle = 'dashed')

Dashdot:

plt.plot(X,Y, linestyle = 'dashdot')

Matplotlib Line colors:

You can use the keyword argument color or the shorter c to set the color of the line:

#Set line color to red
plt.plot(X,Y, color = 'r')

#Hexadeciamal code for colours
plt.plot(X,Y, color = '#FF0000')


Matplotlib Titles and Markers:

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)

plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.show()

With Pyplot, you can use the title() function to set a title for the plot.

plt.title("Sports Watch Data")

Matplotlib Gridlines:

With Pyplot, you can use the grid() function to add grid lines to the plot.

plt.grid()


Matplotlib Subplot:

Display Multiple Plots

With the subplot() function you can draw multiple plots in one figure:

plt.subplot(1, 2, 1)
# this denotes 1 row 2 columns and 1st elemnt
plt.subplot(1, 2, 2)
#this denotes 1 row of 2 columns and 2nd plot or element
import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()

Title each plot with title():

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.show()

Supertitle (Title for entire plotting):

import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.suptitle("MY SHOP")
plt.show()


Matplotlib Bars:

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()

Horizontal Bars:

import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.barh(x,y)
plt.show()

Bar color:

plt.bar(x, y, color = "red")

Matplotlib Scatter Plots:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)
plt.show()

Compare Plots:

import matplotlib.pyplot as plt
import numpy as np

#day one, the age and speed of 13 cars:
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)

#day two, the age and speed of 15 cars:
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)

plt.show()


Matplotlib Histogram:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()


Matplotlib Pie Charts:

import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()

Piechart labels:

import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.show()

Piechart Legends:

import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.legend()
plt.show()


In this comprehensive Matplotlib tutorial, we've embarked on a journey through the intricacies of data visualization in Python. From the fundamental basics of plotting to the intricacies of customizing various chart types, you've gained a solid foundation for creating visually stunning and insightful graphics. Matplotlib's flexibility and ease of use make it an indispensable tool in the arsenal of data scientists and developers alike.

As you venture further into the realm of Matplotlib, remember that practice is key. Experiment with different plot types, customize your visualizations, and explore the vast array of features this library has to offer. Whether you're visualizing trends in data, uncovering patterns, or presenting findings to a broader audience, Matplotlib empowers you to tell compelling stories through your data.

As you continue your data visualization journey, keep the official Matplotlib documentation close at hand and explore advanced topics to further refine your skills. With the knowledge gained from this tutorial, you're well-equipped to elevate your data visualization game and make meaningful contributions in the ever-evolving field of data science. Happy plotting!