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:
ax = plt.subplot()
ax.set_xticks([0, 1, 2, 3, 4, 5, 6, 7, 8])
ax.set_xticklabels(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
rotationkeyword 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:
Let’s add the appropriate labels for the chart you made in the last exercise for the coffee shop, MatplotSip.
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
Set the x-axis ticks to be the numbers from 0 to the length of
Use the strings in the
drinks list for the x-axis ticks of the plot you made with