Skip to main content

Matplotlib

This page provides an introduction to matplotlib. These are my notes from YouTube Video.

Overview

Matplotlib is a popular Python library for data visualization, allowing users to create static, animated, and interactive plots. It provides a wide range of plot types, including line charts, scatter plots, bar charts, histograms, and 3D plots, making it a versatile tool.

# importing numpy and matplotlib

import numpy as np
import matplotlib.pyplot as plt

Scatter Plot

x_data = np.random.random(5) * 100
# o/p: array([12.03317897, 34.1242533 , 65.45238165, 10.47379332, 3.40500133])

y_data = np.random.random(5) * 100
# o/p: array([38.61436355, 83.34639559, 76.48956633, 20.78149964, 75.23153235])

plt.scatter(x_data, y_data, c='gold', marker='*', s=100, alpha=0.9)
plt.title("Random X & Y Values", fontsize=20)
plt.xlabel("x-value")
plt.xticks([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
plt.ylabel("y-value")
plt.yticks([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
plt.show()
matplotlib-1.svg

Line Plot

years = [2014 + x for x in range(11)]
# o/p: [2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024]

weights_boy = [60 + np.random.randint(10, 21) for _ in range(11)]
# o/p: [75, 78, 78, 72, 80, 73, 73, 71, 74, 79, 78]

weights_girl = [60 + np.random.randint(10, 21) for _ in range(11)]
# o/p: [76, 75, 70, 73, 76, 73, 77, 80, 80, 80, 74]

plt.plot(years, weights_boy, c='blue', lw=2, linestyle='--', label='Boys')
plt.plot(years, weights_girl, c='pink', lw=2, linestyle='--', label='Girls')
plt.legend()
plt.show()
matplotlib-2.svg

Bar Plot

x = ["C++", "C#", "Python", "Java", "Go"]
y = [20, 50, 60, 70, 10]

plt.bar(x, y, align='edge', width=0.5, edgecolor='green', lw=2)
plt.show()
matplotlib-3.svg

Histogram Plot

ages = np.random.normal(20, 1.5, 5)
# array([21.35891384, 21.52995301, 18.2369266 , 18.90032423, 22.22981015])

# histogram
plt.hist(ages, bins=[ages.min(), 20, ages.max()])
plt.show()

# cummulative histogram
plt.hist(ages, bins=[ages.min(), 20, ages.max()], cumulative=True)
plt.show()

Histogram

matplotlib-4.svg

Cummulative Histogram

matplotlib-5.svg

Pie Chart

langs = ["Python", "C++", "Java", "C#", "Go"]
votes = [45, 25, 15, 5, 10]
explodes = [0.0, 0.0, 0.0, 0.3, 0.0]

plt.pie(votes, labels=langs, explode=explodes,
autopct='%0.0f%%', pctdistance=0.7)
plt.show()
matplotlib-6.svg

Boxplots

height = np.random.normal(170, 5, 10)
# o/p:
# array([174.22305306, 172.9200744 , 170.89283148, 179.10659081,
# 175.46530681, 169.73610812, 167.3291594 , 168.16128635,
# 170.91930996, 164.48483597])

plt.boxplot(height)
plt.show()
matplotlib-7.svg

Multiple Plots

x1, y1 = np.random.random(100), np.random.random(100)
x2, y2 = np.arange(100), np.random.random(100)

plt.figure(1)
plt.scatter(x1, y1)

plt.figure(2)
plt.plot(x2, y2)

plt.show()
matplotlib-8.svg
matplotlib-9.svg

SubPlots

x = np.arange(100)

fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(x, np.sin(x))
axs[0, 0].set_title("Sine Wave")

axs[0, 1].plot(x, np.cos(x))
axs[0, 1].set_title("Cosine Wave")

axs[1, 0].plot(x, np.random.random(100))
axs[1, 0].set_title("Random Function")

axs[1, 1].plot(x, np.log(x))
axs[1, 1].set_title("Log Function")

plt.show()
matplotlib-10.svg

3D Plots

ax = plt.axes(projection='3d')

x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)

ax.scatter(x, y, z)
ax.set_title("3d Plot")

plt.show()
matplotlib-11.svg