Matplotlib: build a chart that answers a question
Choose a chart, label the units and check that its visual scale represents the data fairly.
Start the worked examples ↓Show customer totals with a labelled bar chart
This standalone example uses fictional completed-order values in USD. It writes a PNG to your working directory. Run it in a Python environment with Matplotlib installed.
import matplotlib.pyplot as plt
customers = ['Customer 1', 'Customer 2', 'Customer 3']
revenue = [50, 80, 20]
fig, ax = plt.subplots(figsize=(7, 4), layout='constrained')
ax.bar(customers, revenue, color='#5B50E6')
ax.set(title='Completed order value in the sample', ylabel='Amount (USD)')
ax.set_ylim(bottom=0)
ax.spines[['top', 'right']].set_visible(False)
fig.savefig('customer-revenue.png', dpi=160)
plt.show()Expected: three bars at $50, $80 and $20. Customer 2 is highest. These observations describe this sample only; they do not establish a general customer-spending pattern.
Understand Figure and Axes
The Figure is the overall canvas. An Axes is a plotting area with its own scales and labels. Keeping the ax handle makes it clear which subplot a change belongs to. A grid of subplots returns several Axes objects; inspect their shape before indexing them.
Match the chart to the question
- Bar: compare categories. Start the value axis at zero when bar length encodes magnitude.
- Line: show an ordered progression, often time. Sort dates and make missing periods visible.
- Scatter: inspect paired numerical observations; a pattern does not establish causation.
- Histogram: show a distribution. Check how the bin choice changes its appearance.
Make the chart readable
Use a descriptive title, explicit units and labels that fit the exported size. Do not rely on colour alone to distinguish series: add markers, line styles or direct labels. A logarithmic axis needs a clear explanation and suitable values. Avoid unnecessary 3D decoration and misleading dual axes.
Save using the figure handle and inspect the exported image for clipped text. A larger DPI does not fix labels that are too small relative to the figure. For automated batches, close figures after saving to release resources.
Practise the explanation
Why use bars here?
We are comparing three category totals, so bar length makes the comparison direct. A line would imply a meaningful sequence between customers that the data does not contain.
Does every Python chart library use Matplotlib?
No. Matplotlib is one plotting library and is used by tools such as Seaborn. Other libraries can use different rendering systems. Choose based on the output and interaction you need.
Continue learning
Reference: Matplotlib quick start. Check behaviour for your installed version.
These are educational exercises with fictional data, not a verified employer question bank. Amounts in examples are USD unless otherwise stated.
Practise your explanation
Use the self-practice guide to review your approach. Mock-interview booking and payment are currently inactive. All planned preparation sessions and feedback are in English.
View preparation options