Finally, let’s fix up a couple loose ends on this graph to make it look finished and intentional. Our lines, title, and legend are good to go, but the axes aren’t labeled, and the overlapping tick labels (the months of the year) are hard to read.
Axis labels are modified using a general function, so can be easily added onto any graph with an x or y-axis by using the
plt.ylabel() functions. Just like
plt.title(), these functions take a string as the input.
Most line charts will have some kind of time shown on the x-axis, so the label might read
plt.xlabel(‘Date’). A y-axis label, for example, could be
plt.ylabel('Vacuum Cleaner Sales ($)') or
plt.ylabel('Population of Boston').
Tick labels are also modified using a general function: in this case, we’ll use
plt.tick_params() (short for “tick parameters”) to change the rotation of the tick labels. This function takes many different parameters to adjust tick marks and tick labels: position above or below the axes, length, width, and color of tick marks, as well as the font size, color, and rotation of tick labels, among other parameters. We can also use
.tick_params() to create gridlines.
plt.tick_params(axis='x', direction='out', color='red', labelsize='large', labelcolor='purple', labelrotation=30)
The above parameters will make red tickmarks outside the x-axis, with purple labels in a large font size, rotated 30 degrees. Sounds dramatic!
Label the y-axis “Temperature (F)”.
Label the x-axis “Month”.
Rotate the tick-labels on the x-axis 45 degrees.