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.
- Create a figure and plot revenue by week with plt.plot.
- Plot the target as a second series using a dashed line style.
- Set title, axis labels, legend, and save the figure to kpi_trend.png.
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
- Python documentationOfficial reference for Python syntax, standard library modules, and tutorials.
- pandas documentationOfficial reference for pandas DataFrame operations, indexing, and analysis APIs.
- Matplotlib documentationOfficial reference for plotting with Matplotlib and pyplot.