Eventually, we will want to show these plots to other people to convince them of important trends in our data. When we do that, we’ll want to make our plots look as professional as possible.
The first step towards a professional-looking plot is adding labels to the x-axis and y-axis, and giving the plot a title.
We can label the x- and y- axes by using plt.xlabel()
and plt.ylabel()
.
The plot title can be set by using plt.title()
.
All of these commands require a string, which is a set of characters in either single ('
) or double ("
) quotes.
"This is a string" 'This is also a string' 'This is NOT a string (the quotes do not match)"
For example, if someone has been keeping track of their happiness (on a scale out of 10) throughout the day and wants to display this information with labeled axes, we can use the following commands:
hours = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] happiness = [9.8, 9.9, 9.2, 8.6, 8.3, 9.0, 8.7, 9.1, 7.0, 6.4, 6.9, 7.5] plt.plot(hours, happiness) plt.xlabel('Time of day') plt.ylabel('Happiness Rating (out of 10)') plt.title('My Self-Reported Happiness While Awake') plt.show()
This will result in a labeled graph:
Now, you can try adding labels to plots of your own.
Instructions
Label the x-axis 'Time'
.
Label the y-axis 'Dollars spent on coffee'
.
Add the title 'My Last Twelve Years of Coffee Drinking'
.