Matplotlib Plotting Basics
Creating visualizations with Matplotlib.
Basic Plot
Import
import matplotlib.pyplot as plt
Line plot
plt.plot(x, y)
plt.show()
With labels
plt.plot(x, y, label=Line 1)
plt.xlabel(X axis)
plt.ylabel(Y axis)
plt.title(My Plot)
plt.legend()
plt.show()
Plot Types
Scatter plot
plt.scatter(x, y)
Bar plot
plt.bar(categories, values)
Histogram
plt.hist(data, bins=20)
Pie chart
plt.pie(sizes, labels=labels)
Subplots
Create subplots
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 1].scatter(x, y)
plt.show()
Styling
Line style
plt.plot(x, y, linestyle=dashed, color=red)
Markers
plt.plot(x, y, marker=o)
Grid
plt.grid(True)