Skip to content
wannacode

Which chart for which question

In short: The kind of chart is chosen by the question: sorted bars for a comparison, a line for change over time, and bars again, not a pie, for a share of the whole.

Problem

Four questions, one set of data

The manager has four questions about the year:

  1. Which products bring in the most money?
  2. How does the revenue go day after day?
  3. What part of the revenue is each category?
  4. What is a usual order?

The data is the same: the orders and their lines. The chart, though, is different for each question. There is no one chart that answers them all. There is a chart that answers one question well, and many that answer it badly.

The idea

The question first, then the chart

Do not start from “I want a pie” or “I want a line”. Start from the question and see what kind it is:

  • comparison (“which is more”): bars sorted by value; with long names horizontal (ax.barh);
  • change over time (“how is it going”): a line;
  • share of a whole (“what part”): sorted bars again, in percent; a pie is harder to read;
  • distribution (“what is usual, how much does it spread”): a histogram, in the next lesson;
  • relationship (“do they grow together”): a scatter plot, also in the next lesson.

Let us see the first three with the same data.

The idea

Comparison: sorted horizontal bars

“Which products bring in the most?” is a comparison of 25 products. Their names are long, so the bars are horizontal: ax.barh(names, values). The names sit on the left and can be read without rotating them.

Sorting with sort_values() makes the chart a ranking. barh draws the first row at the bottom, so ascending order puts the biggest at the top. figsize=(7, 7) gives the figure a size in inches: 25 bars need height.

Pythondata: the shop tables

White cheese brings in the most: €10,325.30. After it come coffee beans (€9,585.10) and kashkaval cheese (€8,616.50). Sorted, the bars answer the question before you read the numbers.

More precisely, if you are curious

Without sort_values() the products come in alphabetical order from groupby. Alphabetical order is handy when you look for one product by name. For the question “which is the most” you need order by value.

Try it

The best sellers in units

Which 10 products sold the most units in the year? Store them in top (the product is the index, the units are the value) and draw them as horizontal bars with the biggest at the top.

Pythondata: the shop tables

The idea

Change over time: a line

“How does the revenue go day after day?” is change over time. A line joins every day to the next one, and the eye follows the direction. 365 bars would be a fence in which nothing can be seen.

The daily revenue jumps because of the weekly rhythm. A second line with the 7-day average (rolling(7) from the lesson on dates) shows the direction under the noise:

Pythondata: the shop tables

There are 365 days. The strongest is 26 December 2025 with €852.90. The thin line zigzags every week, and the thick one shows the big peak in December and the dips in January and September. Along the bottom are dates: ordered_at has been turned into dates with pd.to_datetime, and matplotlib puts in the months by itself.

The idea

Small charts side by side

In the previous lesson the seven categories tangled in one axes. Another way is to put each category in its own small chart and set the charts side by side. These are called small multiples.

plt.subplots(2, 3) gives a figure with 2 rows and 3 columns of axes. axes is a NumPy array of shape (2, 3), and axes.flat hands them out one by one. sharey=True gives them all the same vertical scale. fig.suptitle is a title for the whole figure. Along the bottom there is only the number of the month (pv.index.str[5:]), so it fits:

Pythondata: the shop tables

With a shared scale the heights can be compared: dairy is highest all year, and kitchen is low with one peak in December. Each line reads on its own, with no legend and no colours to tell them apart.

The idea

Share of a whole: bars against a pie

“What part of the revenue is each category?” is a question about shares. It is often drawn as a pie chart. Here are the pie and the sorted bars with the same numbers, side by side. plt.subplots(1, 2) gives two axes: here they are called left and right:

Pythondata: the shop tables

The pie shows that dairy is the biggest slice, a little over a quarter. Cleaning (9.8%) and kitchen (10.2%), though, look the same. Bread (11.9%) is close to them too. The bars on the right put them in order, and the differences show.

1. Look only at the pie on the left. Which is bigger: kitchen or cleaning?

The idea

Why a pie misleads

Bars are compared by length from the same line. The eye does that well. A pie makes the eye compare the angles and areas of slices that are turned in different directions. The eye does that badly, especially when the shares are close.

A pie has other problems too:

  • with more than 4–5 slices the labels pile up;
  • two pies side by side (December against the year) can hardly be compared;
  • colour is the only thing that links a slice to its name.

A pie does its job in one case: two or three slices and the question “more or less than half?”. For everything else sorted bars say the same thing more clearly.

The idea

Distribution and relationship

Two kinds of question are left. They need charts that come in the next lesson:

  • distribution: “what is a usual order?” The answer is not one number but how 10,459 orders spread: how many are small and how many are large. A histogram shows that;
  • relationship: “are orders with more lines more expensive?” Every order becomes a point with two numbers. The cloud of points is a scatter plot.
1. The manager asks: “What is a usual order: are most of them around €5, or around €20?” Which chart answers?
2. “Are orders with more lines more expensive?” Which chart answers?

Exercise

What December brings

December is the strongest month. Does it have more orders, or bigger ones? Store in orders_n the number of orders in every month (without the cancelled ones) and in avg the average order in euros, and draw them as two lines in two axes side by side, each with a title.

Pythondata: the shop tables

What part of the revenue in December 2025 is each category, with the gift card? Store the share in percent in share, sorted, and draw it as horizontal bars with the biggest at the top.

Pythondata: the shop tables

Test

Check yourself

1. You want to compare the revenue of 25 products with long names. What reads best?
2. You drew top = units.nlargest(10) with ax.barh. Why is the best-selling product at the bottom?
3. The number of orders and the average order in euros by month. Why in two axes side by side and not in one?
4. What does sharey=True do in plt.subplots(2, 3, sharey=True)?
5. What is the first thing you decide before you draw a chart?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·