Draw a stacked bar chart

日本語のページ

This page explains how to display a stacked bar chart.

1. Create the source data for the chart

Enter the source data for the chart in an Excel sheet.

As shown in the image below, enter the values ​​to be used as labels for the X-axis and the values ​​to be displayed as a bar chart.

Example of source data for bar chart

2. Add blocks

Follow the steps on the "Creating and displaying charts" page to add the minimum required blocks.
Also, drag the "Clear stacked bar chart height calculation buffer" block after the block that initializes the bar chart.

Clear the buffer for calculating the height of stacked bar chart

3. Repeat according to the cell range of values

Use the block in the "Repeat" group that repeats while changing the variable value by specifying a range to repeat according to the cell range of values.

In the case of the Excel sheet above, the values ​​are in column B (2nd column) to column D (4th column), so use a repeat that changes the variable value from 2 to 4 by 1.

Repeat changing the variable value from 2 to 4

Put this block between the chart initialization block and the chart display block.

4. Repeat creating a bar chart

Put a bar chart creation block inside the loop.

Block to display bar chart

Then, set the first parameter to "Stacked vertical bar" or "Stacked horizontal bar".

This block is long horizontally, so it may be difficult to use if the width of the screen is narrow.
In that case, right-click the block and click "External input" in the menu to change it to a vertical shape.
You can also right-click a vertical block and click "Inline input" in the menu to change it to a horizontal shape.

Also, make the colors of each stacked series different so that you can distinguish them.
If you specify the "null" block in the "Logic" group for the "Line color" and "Interior color" parameters, you can use matplotlib's default color combination.

null block

If you want to specify the colors yourself, create a list of the colors you want to use in the "Create list using" block in the "List" group before the loop.

Then, specify the colors selected from the list in order as the "Line color" and "Interior color" parameters of the "Create bar chart" block.

5. Examples

Here are two examples, one where colors are not specified and one where they are specified.

5-1. When colors are not specified (using matplotlib's default colors)

The following program is an example of how to display the chart at the top of this page.
A bar chart will be displayed based on the Excel sheet provided in "2. Creating the source data for the chart".

Example of a program that draws a stacked bar chart (colors are not specified)

The file for this example can be downloaded from here.

The Excel data file used in this example can be downloaded from here.

When running this example, open the data file in Excel beforehand.

5-2. Specifying a color

The following program is an example where the previous program has been modified to specify a color.

Example of a program to draw a stacked bar chart (specifying colors)

Before drawing the bar chart, a list containing three colors is assigned to the variables line_colors and inner_colors.
Then, when creating the bar chart, colors are taken from line_colors and inner_colors in order and specified as the line color/inner color parameters.
The variable i is changed from 2 to 4 in the repeat block, so i-1 changes from 1 to 3.
This value is used to extract the first, second, third, and so on, colors from the list.

When you run this example, the following chart will be displayed.

Execution result of an example program that draws a stacked bar chart (when specifying colors)

The file for this example can be downloaded from here.

Also, the Excel data file used in this example can be downloaded from here.
When running this example, make sure you have the data file open in Excel beforehand.

6. Points to note when displaying multiple bar graphs

You can also display multiple bar charts by setting either the vertical or horizontal direction to a number greater than 2 in the block that initializes the chart.
If you include a stacked bar chart, you will need to include a block that "clear height calculation buffer for stacked chart" even after displaying the stacked bar chart.