Chapter 32. Text and Annotation
Creating a good visualization involves guiding the reader so that the figure tells a story. In some cases, this story can be told in an entirely visual manner, without the need for added text, but in others, small textual cues and labels are necessary. Perhaps the most basic types of annotations you will use are axes labels and titles, but the options go beyond this. Let’s take a look at some data and how we might visualize and annotate it to help convey interesting information. We’ll start by setting up the notebook for plotting and importing the functions we will use:
In[1]:%matplotlibinlineimportmatplotlib.pyplotaspltimportmatplotlibasmplplt.style.use('seaborn-whitegrid')importnumpyasnpimportpandasaspd
Example: Effect of Holidays on US Births
Let’s return to some data we worked with earlier, in “Example: Birthrate Data”, where we generated a plot of average births over the course of the calendar year. We’ll start with the same cleaning procedure we used there, and plot the results (see Figure 32-1).
In[2]:# shell command to download the data:# !cd data && curl -O \# https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/# births.csv
In[3]:fromdatetimeimportdatetimebirths=pd.read_csv('data/births.csv')quartiles=np.percentile(births['births'],[25,50,75])mu,sig=quartiles[1],0.74*(quartiles[2]-quartiles[0])births=births.query('(births > @mu - 5 * @sig) &(births<@mu+5*@sig)')births ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access