Matplotlib Guide with Code and Explanations
Basic Line Plot
This is a simple line plot showing y values against x.
# Basic Line Plot
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [10, 12, 5, 8, 7]
[Link](x, y)
[Link]('Simple Line Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link](True)
[Link]()
Bar Plot
Bar plots are used to represent categorical data with rectangular bars.
# Bar Plot
x = ['A', 'B', 'C', 'D']
y = [10, 15, 7, 12]
[Link](x, y, color='skyblue')
[Link]('Bar Chart')
[Link]()
Scatter Plot
Scatter plots show the relationship between two variables.
# Scatter Plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
[Link](x, y, color='red')
[Link]('Scatter Plot')
Matplotlib Guide with Code and Explanations
[Link]()
Histogram
Histograms are used to represent the distribution of a dataset.
# Histogram
import numpy as np
data = [Link](1000)
[Link](data, bins=30, color='purple')
[Link]('Histogram')
[Link]()
Pie Chart
Pie charts show percentages of a whole.
# Pie Chart
labels = ['A', 'B', 'C', 'D']
sizes = [25, 35, 20, 20]
[Link](sizes, labels=labels, autopct='%1.1f%%', startangle=90)
[Link]('equal')
[Link]('Pie Chart')
[Link]()
Subplots
Use subplots to create multiple plots in a single figure.
# Subplots
fig, axs = [Link](2, 2)
x = [1, 2, 3, 4, 5]
y = [10, 12, 5, 8, 7]
axs[0, 0].plot(x, y)
axs[0, 1].bar(x, y)
axs[1, 0].scatter(x, y)
Matplotlib Guide with Code and Explanations
axs[1, 1].hist([Link](100), bins=20)
plt.tight_layout()
[Link]()
Object-Oriented Interface
This is a more flexible way to build plots using Axes and Figure objects.
# OO Interface
fig, ax = [Link]()
[Link](x, y)
ax.set_title('Using OO Interface')
ax.set_xlabel('X')
ax.set_ylabel('Y')
[Link]()