Learn
Different Plot Types
Simple Bar Chart II

When we create a bar chart, we want each bar to be meaningful and correspond to a category of data. In the drinks chart from the last exercise, we could see that sales were different for different drink items, but this wasn’t very helpful to us, since we didn’t know which bar corresponded to which drink.

In the previous lesson, we learned how to customize the tick marks on the x-axis in three steps:

  1. Create an axes object
    ax = plt.subplot()
  2. Set the x-tick positions using a list of numbers
    ax.set_xticks([0, 1, 2, 3, 4, 5, 6, 7, 8])
  3. Set the x-tick labels using a list of strings
    ax.set_xticklabels(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
  4. If your labels are particularly long, you can use the rotation keyword to rotate your labels by a specified number of degrees:
    ax.set_xticklabels(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'], rotation=30)

Note: We have to set the x-ticks before we set the x-labels because the default ticks won’t necessarily be one tick per bar, especially if we’re plotting a lot of bars. If we skip setting the x-ticks before the x-labels, we might end up with labels in the wrong place.

Remember from Lesson I that we can label the x-axis (plt.xlabel) and y-axis (plt.ylabel) as well. Now, our graph is much easier to understand: labeled_planet_chart

Let’s add the appropriate labels for the chart you made in the last exercise for the coffee shop, MatplotSip.

Instructions

1.

The list drinks represents the drinks sold at MatplotSip. We are going to set x-tick labels on the chart you made with plt.bar in the last exercise.

First, create the axes object for the plot and store it in a variable called ax.

2.

Set the x-axis ticks to be the numbers from 0 to the length of drinks.

3.

Use the strings in the drinks list for the x-axis ticks of the plot you made with plt.bar.

Folder Icon

Sign up to start coding

Already have an account?