Let's create our first plot: a bar chart. Bar charts are perfect for comparing values across different groups or categories, like sales figures for different products.
Engagement Message
What's a comparison from your daily life that would make a clear bar chart?
To build a bar chart, you need your data organized into two lists. One list for your categories (the labels for each bar) and a second list for the matching numerical values (the height of each bar). The order must match!
Engagement Message
What two lists would you need to chart the number of books read each month?
In Matplotlib, we use the ax.bar()
method to create a bar chart. You provide your list of categories for the x-axis and your list of values for the y-axis.
ax.bar(category_list, value_list)
The library handles all the drawing for you.
Engagement Message
Makes sense?
Let's see the full code. First, we prepare our data lists. Then, we create our plot and use ax.bar()
to draw the bars.
Engagement Message
What categories and values would you use if you wanted to make a bar chart about your favorite snacks?
Here is the bar chart created by that code. Notice how each product on the x-axis has a bar whose height corresponds to its sales value on the y-axis. This makes comparing them instant.
