Mat Plot Lib
Mat Plot Lib
Matplotlib
It is one of the most popular libraries for data visualiza on in Python.
It provides a flexible and comprehensive toolkit for crea ng a wide variety of sta c,
animated, and interac ve visualiza ons in Python.
Matplotlib is a versa le library that allows you to create a wide range of visualiza ons, from
simple plots to complex mul -plot grids.
With its extensive func onality and customiza on op ons, it is an invaluable tool for anyone
working with data in Python.
Whether you are analyzing data, presen ng findings, or simply exploring datasets, mastering
Matplotlib will significantly enhance your data visualiza on capabili es.
A er installa on, you can import Matplotlib in your Python scripts or Jupyter notebooks:
import numpy as np
import pandas as pd
plt.figure()
# Create an axis
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Subplots
Subplots allow you to create mul ple plots in a single figure. You can specify the number of rows and
columns for the subplots.
Example:
plt.show()
Common Plots
Sca er Plots:
Useful for visualizing the rela onship between two numerical variables.
x = np.random.rand(50)
y = np.random.rand(50)
plt.sca er(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Histograms:
Useful for displaying the distribu on of a numerical variable.
data = np.random.randn(1000)
plt. tle('Histogram')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
Box Plots:
Useful for visualizing the spread and iden fying outliers in data.
plt.ylabel('Value')
plt.show()
5. Logarithmic Scale
You can set logarithmic scales for axes to be er visualize data that spans several orders of
magnitude.
y = np.exp(x)
plt.plot(x, y)
plt.yscale('log')
plt.xlabel('X-axis')
plt.show()
Placement of Ticks and Custom Tick Labels
You can customize ck placement and labels to improve the readability of your plots.
x = np.arange(0, 10, 1)
y = x ** 2
plt.plot(x, y)
plt.x cks( cks=[0, 2, 4, 6, 8, 10], labels=['Zero', 'Two', 'Four', 'Six', 'Eight', 'Ten'])
plt.show()
Pandas Visualiza on
Pandas also provides built-in visualiza on capabili es that leverage Matplotlib.
Example:
df = pd.DataFrame({
'A': np.random.randn(100),
'B': np.random.randn(100)
})
plt.show()
Style Sheets
Matplotlib allows you to change the visual style of your plots using style sheets.
plt.plot(np.random.randn(100).cumsum())
plt.show()
Plot Types
Here are some common plot types available in Matplotlib:
Area Plot: Shows quan ta ve data visually, with the area below the line filled.
plt.show()
Line Plots:
Useful for displaying trends over me.
y = np.sin(x)
plt.plot(x, y)
plt.show()
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.show()
data = np.random.normal(size=1000)
sns.kdeplot(data)
plt.show()
Distribu on Plots
Visualizing the distribu on of data is essen al for understanding its characteris cs. You can use
histograms and KDE together.
sns.histplot(data, kde=True)
plt. tle('Distribu on Plot with KDE')
plt.show()
ps = sns.load_dataset(' ps')
plt.show()
plt.show()
Matrix Plots
Matrix plots, like heatmaps, are useful for visualizing the rela onships between variables.
sns.heatmap(data)
plt. tle('Heatmap')
plt.show()
Regression Plots
Visualizing the rela onship between variables can be enhanced using regression plots.
plt.show()
Grids
Matplotlib provides the ability to create grid layouts for more complex visualiza ons.
axs[0, 0].plot(x, y)
axs[1, 1].boxplot(data)
plt. ght_layout()
plt.show()