Library cheatsheet
Matplotlib and seaborn 3 Cheatsheet
Draw line, scatter, bar and histogram charts with Matplotlib, and statistical charts with seaborn.
Basics
- Install the libraries — Install Matplotlib and seaborn.
- Import pyplot — The conventional import names.
- Draw a line chart — Plot y against x and show the figure.
- Add a title and axis labels — Label the chart so it can be understood on its own.
- Set the figure size — Create a figure with a size in inches and a resolution.
- Save a chart to a file — Write the figure as PNG, SVG or PDF. bbox_inches="tight" trims the margins.
- Draw without a screen — Use the Agg backend on servers and in CI, where there is no display.
Chart types
- Scatter plot — Plot two variables as points, colored by a third.
- Bar chart — Compare values across categories.
- Horizontal bar chart — A better choice when category names are long.
- Histogram — Show the distribution of a numeric variable in 30 bins.
- Box plot — Compare distributions with medians, quartiles and outliers.
- Pie chart — Show parts of a whole. Use sparingly: bars are easier to compare.
- Heatmap with imshow — Show a matrix as colors, with a color scale.
- Error bars — Plot values with uncertainty.
Layout and styling
- Draw several charts — Create a grid of axes and plot on each one.
- Two y-axes — Plot two series with different scales on the same chart.
- Use a log scale — Plot values that span several orders of magnitude.
- Annotate a point — Add a label with an arrow pointing at a data point.
- Apply a style — Use a built-in style sheet for consistent looks.
- Rotate tick labels — Rotate crowded labels on the x-axis.
seaborn
- Set a theme — Apply seaborn's default look to all charts.
- Histogram with density — A histogram with a smooth density curve on top.
- Scatter plot by group — Color points by category with hue.
- Box plot by category — Compare a variable across groups.
- Count categories — Show how many rows fall in each category.
- Correlation heatmap — Show how numeric columns relate, with values written in the cells.
- Plot every pair of variables — A grid of scatter plots and histograms for exploring a dataset.
- Line chart with confidence interval — Plot a trend with an interval around the mean.
Related cheatsheets
- Hugging Face cheatsheet — The hf command line, and Transformers and Datasets code for downloading, running, training and sharing models.
- pandas and NumPy 3 cheatsheet — Load, clean, reshape, group and analyze tabular data with pandas, and work with arrays in NumPy.
- Polars 2 cheatsheet — Fast DataFrames in Python: expressions, filtering, grouping, joins, lazy queries and file formats.
- Prisma ORM cheatsheet — Model your database in a schema, run migrations, and query with the type-safe Prisma Client. Examples use the prisma-client-js generator; check the docs for newer generators.
- PyTorch 2 cheatsheet — Tensors, autograd, models, training loops, saving and speed-ups in PyTorch 2.
- React 19 cheatsheet — Hooks, components and patterns for React 19 in TypeScript or JavaScript.