Installation and Import
Two Interfaces
Matplotlib offers two ways to create charts:
For single charts, use the state-based approach. Switch to the object-oriented approach when building subplot grids.
Your First Plot
Customizing a Plot
You can control titles, labels, limits, grids, legends, and tick marks withplt. commands:
Visualizing Numerical Data
Numerical (continuous) data is best explored using plots that reveal trends, distributions, or correlations.Line Plots — Trends Over Time
Histograms — Distributions
Histograms group continuous values into “bins” to show frequency distribution.Scatter Plots — Correlations
Scatter plots reveal relationships between two continuous variables.Visualizing Categorical Data
Categorical data (discrete groups like departments or courses) is best represented with bar charts.Vertical Bar Chart
Horizontal Bar Chart (barh)
Use barh when category names are long — it prevents the labels from overlapping on the x-axis.
Numerical vs. Categorical — Box Plots
A box plot (whisker plot) compares the distribution of a numerical variable across categories. It displays five statistics: minimum, Q1, median, Q3, and maximum, and highlights outliers.Multi-Panel Layouts with Subplots
When you need multiple charts side by side, switch to the object-oriented interface withplt.subplots().
plt.tight_layout() automatically adjusts spacing between subplots so titles and axis labels don’t overlap. Always call it before plt.show() when working with subplot grids.