Titles, Labels and Annotations
Python · Data Visualization
Theory
Text turns a picture into an explanation. ax.set_title() states what the chart shows, and ax.set_xlabel() and ax.set_ylabel() name each axis with its unit, for example 'Sales (Rs lakh)'. When an Axes has several series, give each a label= when drawing and call ax.legend(); loc='upper left' or loc='best' controls where the box goes.
Annotations point at a specific spot: ax.annotate('Diwali peak', xy=(x, y), xytext=(x2, y2), arrowprops=dict(arrowstyle='->')) writes text at xytext with an arrow to the data point xy, while ax.text(x, y, 'note') places plain text in data coordinates. ax.axhline(y=target, linestyle='--') draws a reference line such as a target. ax.set_xticks() with rotation fixes crowded tick labels, and ax.set_ylim(0, ...) controls the visible range.
Why we use it
A chart without a title, units or legend forces readers to guess. Clear labels and one or two annotations tell them what to look at, which is what makes a chart useful in a report or presentation.
When to use it
Always add a title and axis labels with units. Add a legend only when there are two or more series, and annotate only the points that matter, such as a peak, a dip or a target line. Too many notes clutter the chart.
How it works
- Chart with data drawn
- Add a title that says what the chart shows
- Label both axes with units
- Two or more series? Yes: label= on each, then ax.legend(). No: Skip the legend.
- A point the reader must notice? Yes: ax.annotate with an arrow, or a reference line. No: Keep it clean.
- Self-explanatory chart
Syntax
ax.set_title('...') ax.set_xlabel('...') ax.set_ylabel('...')
ax.plot(x, y, label='...'); ax.legend(loc='best')
ax.annotate('text', xy=(x, y), xytext=(x2, y2), arrowprops=dict(arrowstyle='->'))
ax.axhline(y, linestyle='--') ax.text(x, y, 'note')Example
ax.plot(months, sales, label='2024')
ax.set_title('Monthly sales')
ax.set_ylabel('Rs lakh')
ax.annotate('Diwali peak', xy=(9, 310), xytext=(5, 330), arrowprops=dict(arrowstyle='->'))Key points
- Every chart needs a title and axis labels with units
- Pass label= when drawing, then call ax.legend()
- annotate draws text plus an arrow to a data point
- axhline / axvline add reference lines such as targets
- Annotate the one or two points that carry the message