# Matplotlib Tutorial: A Guide from First Plot to Polished Chart

> A Matplotlib tutorial for Python that starts with fig, ax = plt.subplots() and links to guides on chart types, axes, ticks, legends, subplots, colors and saving figures.

Source: https://www.pythoncharts.com/matplotlib/matplotlib-tutorial/ | Published: 2026-11-27 | Updated: 2026-11-27 | Author: Alex Scott | Category: Matplotlib | Tags: matplotlib, guide, line plot, bar chart, subplots, legend

To make a chart in Matplotlib, create a figure with `fig, ax = plt.subplots()`, draw on the axes with a method such as `ax.plot()` or `ax.bar()`, and call `plt.show()`. This page is the map to the Matplotlib tutorials on this site, grouped by what you're trying to do. Newer ones are written for Matplotlib 3.11.

![Grid of twelve Matplotlib charts: line, grouped bar, scatter, histogram, heatmap, subplot layout, contour, 3D, radar, stream graph, bubble and bump charts](https://www.pythoncharts.com/matplotlib/matplotlib-tutorial/images/matplotlib-tutorial-montage.png)

## Quick Example

```python
import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
online = [31, 35, 42, 40, 51, 58]
in_store = [48, 44, 45, 41, 39, 38]

fig, ax = plt.subplots(figsize=(7, 4.5))
ax.plot(months, online, marker="o", label="Online")
ax.plot(months, in_store, marker="s", label="In store")
ax.set_title("Orders by Channel")
ax.set_ylabel("Orders (thousands)")
ax.legend()
plt.show()
```

![Matplotlib line chart in which online orders rise from 31 to 58 thousand while in-store orders fall from 48 to 38 thousand](https://www.pythoncharts.com/matplotlib/matplotlib-tutorial/images/matplotlib-tutorial-quick-example.png)

## Where Should I Start With Matplotlib?

| Step | Question | Tutorial |
|---|---|---|
| 1. Objects | What are `fig`, `ax` and `plt`? | [Figure vs Axes](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/) |
| 2. First chart | How do I draw and style lines? | [Line plots](https://www.pythoncharts.com/matplotlib/matplotlib-line-plot/) |
| 3. Label it | How do I add titles and axis labels? | [Titles and subtitles](https://www.pythoncharts.com/matplotlib/matplotlib-title-subtitle/) and [axis labels](https://www.pythoncharts.com/matplotlib/matplotlib-axis-labels/) |
| 4. Explain it | How do I add a legend? | [Legends](https://www.pythoncharts.com/matplotlib/matplotlib-legend/) |
| 5. Combine | How do I put several plots in one figure? | [Subplots](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/) |
| 6. Export | How do I save it as a file? | [savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/) |

Keep the [Matplotlib cheat sheet](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/) open as you go, and read [plot not showing](https://www.pythoncharts.com/matplotlib/matplotlib-plot-not-showing/) if no window appears.

## Which Matplotlib Chart Should I Use?

To choose by appearance, browse the [Python Chart Gallery](https://www.pythoncharts.com/chart-gallery/).

| To show | Method | Tutorial |
|---|---|---|
| A trend over time | `ax.plot` | [Line plots](https://www.pythoncharts.com/matplotlib/matplotlib-line-plot/) |
| Totals over time | `ax.stackplot` | [Area charts](https://www.pythoncharts.com/matplotlib/matplotlib-area-chart/), [stream graphs](https://www.pythoncharts.com/matplotlib/matplotlib-streamgraph/) |
| Values by category | `ax.barh` | [Sorted horizontal bars](https://www.pythoncharts.com/matplotlib/matplotlib-horizontal-bar-chart/), [styled bar charts](https://www.pythoncharts.com/matplotlib/beautiful-bar-charts-matplotlib/) |
| Several series by category | `ax.bar` | [Grouped bars](https://www.pythoncharts.com/matplotlib/grouped-bar-charts-matplotlib/), [stacked bars with labels](https://www.pythoncharts.com/matplotlib/stacked-bar-charts-labels/) |
| Bars colored by a rule | `ax.bar(color=...)` | [Color bars by value](https://www.pythoncharts.com/matplotlib/matplotlib-bar-chart-color-by-value/) |
| A cleaner ranking | `ax.hlines`, `ax.stem` | [Lollipop and dot plots](https://www.pythoncharts.com/matplotlib/matplotlib-lollipop-chart/) |
| Rank changes | `ax.plot` | [Bump charts](https://www.pythoncharts.com/matplotlib/matplotlib-bump-chart/) |
| Before and after | `ax.plot` | [Slope charts](https://www.pythoncharts.com/matplotlib/slope-chart-two-time-points/) |
| A value against a target | `ax.barh` | [Bullet charts](https://www.pythoncharts.com/matplotlib/matplotlib-bullet-chart/) |
| The biggest causes | `ax.bar`, `ax.twinx` | [Pareto charts](https://www.pythoncharts.com/matplotlib/pareto-chart-python/) |
| Parts of a whole | `ax.pie` | [Pie charts](https://www.pythoncharts.com/matplotlib/pie-chart-matplotlib/), [donut charts](https://www.pythoncharts.com/matplotlib/donut-chart-center-text/) |
| Shares across two categories | `ax.bar` | [Marimekko and mosaic charts](https://www.pythoncharts.com/matplotlib/marimekko-mosaic-chart-python/) |
| One distribution | `ax.hist` | [Histograms](https://www.pythoncharts.com/matplotlib/histograms/) |
| Distributions by group | `ax.boxplot` | [Box plots](https://www.pythoncharts.com/matplotlib/box-plots-matplotlib/) |
| Ages by sex | `ax.barh` | [Population pyramids](https://www.pythoncharts.com/matplotlib/population-pyramid-matplotlib/) |
| Two numeric variables | `ax.scatter` | [Scatter plots](https://www.pythoncharts.com/matplotlib/matplotlib-scatterplot/) |
| A third variable as size | `ax.scatter(s=...)` | [Bubble charts](https://www.pythoncharts.com/matplotlib/matplotlib-bubble-chart/) |
| Uncertainty | `ax.errorbar` | [Error bars](https://www.pythoncharts.com/matplotlib/matplotlib-error-bars/) |
| A grid of values | `ax.imshow` | [Heatmaps](https://www.pythoncharts.com/matplotlib/heatmaps-matplotlib/), [correlation matrices](https://www.pythoncharts.com/matplotlib/correlation-matrix-heatmap-annotations/) |
| A surface in 2D | `ax.contourf` | [Contour plots](https://www.pythoncharts.com/matplotlib/matplotlib-contour-plot/) |
| Three dimensions | `projection="3d"` | [3D plots](https://www.pythoncharts.com/matplotlib/matplotlib-3d-plots/) |
| Several measures per item | polar axes | [Radar charts](https://www.pythoncharts.com/matplotlib/radar-charts/) |
| Many small trends | `plt.subplots` | [Sparklines and small multiples](https://www.pythoncharts.com/matplotlib/sparklines-python/) |
| An image or array | `ax.imshow` | [Display images](https://www.pythoncharts.com/matplotlib/matplotlib-imshow-display-image/) |
| Density of many points | `ax.hexbin`, `ax.hist2d` | [Hexbin and 2D histograms](https://www.pythoncharts.com/matplotlib/matplotlib-hexbin-2d-histogram/) |
| Values that hold, then jump | `ax.step`, `ax.stem` | [Step and stem plots](https://www.pythoncharts.com/matplotlib/matplotlib-step-stem-plots/) |
| A vector field | `ax.quiver`, `ax.streamplot` | [Quiver and stream plots](https://www.pythoncharts.com/matplotlib/matplotlib-quiver-streamplot/) |
| Exact numbers with the chart | `ax.table` | [Add a table](https://www.pythoncharts.com/matplotlib/matplotlib-table/) |

## Axes, Limits, and Ticks

| You want to | Tutorial |
|---|---|
| Set the x and y range | [Axis limits](https://www.pythoncharts.com/matplotlib/matplotlib-axis-limits-xlim-ylim/) |
| Match the x and y scales | [Aspect ratio and equal axes](https://www.pythoncharts.com/matplotlib/matplotlib-aspect-ratio/) |
| Skip an empty range | [Broken axis](https://www.pythoncharts.com/matplotlib/matplotlib-broken-axis/) |
| Flip an axis | [Invert an axis](https://www.pythoncharts.com/matplotlib/matplotlib-invert-axis-direction/) |
| Use a log scale | [Logarithmic scales](https://www.pythoncharts.com/matplotlib/matplotlib-logarithmic-axis-scale/) |
| Plot two scales on one chart | [Dual y-axes](https://www.pythoncharts.com/matplotlib/matplotlib-dual-y-axes-twinx/) |
| Cross the axes at zero | [Center the spines](https://www.pythoncharts.com/matplotlib/matplotlib-center-axis-spines/) |
| Control where ticks go | [Ticks and tick frequency](https://www.pythoncharts.com/matplotlib/matplotlib-set-ticks/) |
| Rotate crowded labels | [Rotate axis labels](https://www.pythoncharts.com/matplotlib/rotating-axis-labels/) |
| Resize or restyle tick labels | [Tick label font size](https://www.pythoncharts.com/matplotlib/matplotlib-tick-label-font-size-style/) |
| Remove ticks or labels | [Hide ticks and labels](https://www.pythoncharts.com/matplotlib/matplotlib-hide-axis-ticks-labels/) |
| Show dollars or percentages | [Currency and percentage formats](https://www.pythoncharts.com/matplotlib/matplotlib-currency-percentage-axis-labels/) |
| Format dates | [Date formatting](https://www.pythoncharts.com/matplotlib/matplotlib-custom-date-formatting/) |

## Legends, Text, and Annotations

| You want to | Tutorial |
|---|---|
| Move the legend off the data | [Legend outside the plot](https://www.pythoncharts.com/matplotlib/matplotlib-legend-outside/) |
| Reorder or rename entries | [Legend order and names](https://www.pythoncharts.com/matplotlib/matplotlib-legend-order-label-names/) |
| Spread entries across columns | [Multi-column legends](https://www.pythoncharts.com/matplotlib/matplotlib-multi-column-legend/) |
| Change the legend title or font | [Legend title and font size](https://www.pythoncharts.com/matplotlib/matplotlib-legend-title-font-size/) |
| Add a source line or footnote | [Title, subtitle and footnotes](https://www.pythoncharts.com/matplotlib/style-title-subtitle-footnotes-matplotlib/) |
| Label individual points | [Annotate data points](https://www.pythoncharts.com/matplotlib/matplotlib-annotate-data-points/) |
| Point at something | [Annotations with arrows](https://www.pythoncharts.com/matplotlib/matplotlib-annotate-with-arrows/) |
| Write equations | [LaTeX in labels](https://www.pythoncharts.com/matplotlib/matplotlib-latex-equations-labels/) |
| Mark a threshold | [Horizontal and vertical lines](https://www.pythoncharts.com/matplotlib/matplotlib-horizontal-vertical-lines/) |
| Shade an event or period | [Highlight a date range](https://www.pythoncharts.com/matplotlib/matplotlib-highlight-date-range-area/) |

## Subplots and Figure Layout

| You want to | Tutorial |
|---|---|
| Change the figure size | [Figure size](https://www.pythoncharts.com/matplotlib/figure-size-matplotlib/) |
| Make panels of different sizes | [subplot_mosaic and GridSpec](https://www.pythoncharts.com/matplotlib/matplotlib-subplot-mosaic-gridspec/) |
| Share an axis between panels | [Shared axes](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/) |
| Zoom in on part of a chart | [Inset and zoomed axes](https://www.pythoncharts.com/matplotlib/matplotlib-inset-zoom-axes/) |
| Stop labels overlapping | [Subplot spacing](https://www.pythoncharts.com/matplotlib/matplotlib-tight-layout-subplot-spacing/) |

## Colors and Styling

| You want to | Tutorial |
|---|---|
| Pick colors or a colormap | [Colormaps and colors](https://www.pythoncharts.com/matplotlib/matplotlib-colors-colormaps/) |
| Use brand colors | [Hex and RGB colors](https://www.pythoncharts.com/matplotlib/matplotlib-custom-hex-colors-rgb-palettes/) |
| Build your own colormap | [Custom colormaps](https://www.pythoncharts.com/matplotlib/matplotlib-custom-colormaps/) |
| Add or adjust a color scale | [Colorbars](https://www.pythoncharts.com/matplotlib/matplotlib-colorbar/) |
| Change markers | [Markers](https://www.pythoncharts.com/matplotlib/matplotlib-markers/) |
| Style the gridlines | [Grids](https://www.pythoncharts.com/matplotlib/customizing-grid-matplotlib/) |
| Add stripes or dots to fills | [Hatch patterns](https://www.pythoncharts.com/matplotlib/matplotlib-hatch-patterns/) |
| Remove the box around the plot | [Remove spines](https://www.pythoncharts.com/matplotlib/matplotlib-remove-spines/) |
| Fade the area under a line | [Gradient fills](https://www.pythoncharts.com/matplotlib/matplotlib-gradient-fill-under-line/) |
| Use a different font | [Custom fonts](https://www.pythoncharts.com/matplotlib/custom-fonts-matplotlib/) |
| Restyle in one line | [Style sheets](https://www.pythoncharts.com/matplotlib/matplotlib-built-in-style-sheets/) |
| Make a dark chart | [Dark mode](https://www.pythoncharts.com/matplotlib/matplotlib-dark-mode/) |
| Set your own defaults | [rcParams](https://www.pythoncharts.com/matplotlib/matplotlib-rcparams-defaults/) |

## Saving, Animating, and Embedding

| You want to | Tutorial |
|---|---|
| Export PNG, SVG or PDF | [savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/) |
| Export without a background | [Transparent backgrounds](https://www.pythoncharts.com/matplotlib/matplotlib-transparent-background-png-export/) |
| Save a GIF or MP4 | [Animations](https://www.pythoncharts.com/matplotlib/how-to-animate-charts-and-save-as-gif-mp4-in-matplotlib/) |
| Put a chart in a desktop app | [Tkinter and PyQt](https://www.pythoncharts.com/matplotlib/embed-matplotlib-tkinter-pyqt/) |
| Zoom and add sliders in a notebook | [Interactive Jupyter plots](https://www.pythoncharts.com/matplotlib/matplotlib-interactive-jupyter/) |

## Should I Use pyplot or the Object-Oriented Interface?

`plt.plot()` draws on whichever axes is current, which is fine for a quick single chart. `ax.plot()` names the axes it draws on, so it stays clear with several panels or when a function builds a chart. Most method names carry over with a `set_` prefix: `plt.title()` becomes `ax.set_title()`. [Figure vs Axes](https://www.pythoncharts.com/matplotlib/matplotlib-figure-vs-axes/) has the full translation table.

## Common Matplotlib Problems

| Problem | Fix | Details |
|---|---|---|
| No window or inline chart appears | Call `plt.show()` and check the backend | [Plot not showing](https://www.pythoncharts.com/matplotlib/matplotlib-plot-not-showing/) |
| The saved image is blank | Call `fig.savefig()` before `plt.show()` | [savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/) |
| Labels are cut off in the saved file | Pass `bbox_inches="tight"` | [savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/) |
| Subplot titles and labels overlap | Pass `layout="constrained"` to `plt.subplots()` | [Subplot spacing](https://www.pythoncharts.com/matplotlib/matplotlib-tight-layout-subplot-spacing/) |
| The legend covers the data | Move it with `bbox_to_anchor` | [Legend outside](https://www.pythoncharts.com/matplotlib/matplotlib-legend-outside/) |
| X labels run together | `ax.tick_params(axis="x", labelrotation=45)` | [Rotate labels](https://www.pythoncharts.com/matplotlib/rotating-axis-labels/) |

## Similar Topics

- [Seaborn Tutorial: statistical charts from DataFrames](https://www.pythoncharts.com/seaborn/seaborn-tutorial/)
- [Plotly Tutorial: interactive charts](https://www.pythoncharts.com/plotly/plotly-tutorial/)
- [Python Interactive Charting Libraries Compared](https://www.pythoncharts.com/python/python-interactive-charting-libraries-compared/)
