Skip to Content
Learn
Recreate Graphs using Matplotlib!
Stacked Bars

Now, we are going to look at the chart called stacked-bars.png. This graph displays the breakdown of students who got As, Bs, Cs, Ds, and Fs in each unit.

stacked

The data you will need to recreate this chart is in the lists As, Bs, Cs, Ds, Fs, and unit_topics.

Save your recreated chart to a file called my_stacked_bar.png.

Instructions

1.

The Bs bars will go on top of the As bars, but at what heights will the Cs, Ds, and Fs bars start?

The bottom of the bars representing the Cs will be at the height of the As plus the Bs. We can do this in NumPy (a scientific computing package for Python) with the np.add function. c_bottom, the starting heights for the Cs, will be:

c_bottom = np.add(As, Bs)

Underneath the definition of c_bottom, define d_bottom (where the Cs end), and f_bottom (where the Ds end).

2.

Create a figure of width 10 and height 8.

3.

Plot the As, Bs, Cs, Ds, and Fs. Give each one the appropriate bottom list that will stack them on top of each other.

4.

Create a set of axes and save them to ax.

5.

Set the x-ticks to be range(len(unit_topics)).

6.

Set the x-tick labels to be the unit_topics.

7.

Give the plot the title you see in the final graph, and the same x-axis label and y-axis label.

8.

Save your figure to a file called my_stacked_bar.png.

Folder Icon

Sign up to start coding

Already have an account?