Python charting guide

Python Data Visualization with Matplotlib

Create Matplotlib line and bar charts from pandas summaries, with sorted axes, clear labels, and target lines that explain KPI gaps at a glance.

How this page is maintained

Written for learners, checked against the sources below, and reviewed every year. Last reviewed July 22, 2026.

Short answer

Use Matplotlib to match chart type to question: line for trends over time, bars for category comparison. Label axes clearly, include units, and avoid formatting choices that distort interpretation.

  • Pick chart type from the comparison task, not visual preference.
  • Always label axes and include units in titles.
  • Keep styling simple so values remain easy to compare.

Move from summary table to chart-ready data

Use a grouped summary table from pandas as the chart input layer. Sort time values before plotting lines and sort categories when plotting bars to improve readability.

For weekly KPI reporting, a line chart can show total revenue trend while a bar chart can compare channels for the latest week.

Add context that supports decisions

A chart should show why a value matters. Add a target line, annotate major outliers, and keep the legend close to plotted elements.

  • Use plt.tight_layout to avoid clipped labels.
  • Use consistent color meaning across multiple charts.
  • Export figures with explicit size and dpi for reports.

Plot weekly revenue and a target line

A DataFrame has weekly revenue totals and a constant weekly target value.

  1. Create a figure and plot revenue by week with plt.plot.
  2. Plot the target as a second series using a dashed line style.
  3. Set title, axis labels, legend, and save the figure to kpi_trend.png.
Result: The output chart shows trend direction and target gaps in a format that can be shared in reports.

Common mistakes

  • Using unsorted time values, which creates misleading line paths.
  • Removing axis labels and forcing readers to guess units.
  • Using too many colors without stable meaning across charts.
  • Cropping labels because figure size and layout were not set.

Try one

When comparing values over time in Python, which default chart type usually communicates trend best?

A line chart, because it preserves sequence and direction across periods.

Sources

Learn this with a tutor

Tell LearnLive what you already know and what you need to do with data visualization.

Build this course