# Jupyter Notebook Templates for Data Science: Plotting

*July 27, 2020* | #data-visualization, #data-science, #jupyter, #my-projects

I recently released my [Jupyter Notebook Template Library][library]. Its goal
is to accelerate your data science projects without having to spend hours
poring over old notebooks to find handy code snippets. In this post I dive
into the plotting notebook to show you what it can do.

[library]: https://github.com/agude/Jupyter-Notebook-Template-Library

[nb_post]: /blog/jupyter-not-for-development/

## The Plotting Notebook

Visualizing your data is a critical step in understanding it, and so it is
appropriate that the [**first notebook in the library**][plotting_nb] helps
with making beautiful plots.

[plotting_nb]: https://github.com/agude/Jupyter-Notebook-Template-Library/blob/d6cda39c388154cb8f4073e669efff109c743a99/notebooks/basic-plotting-template.ipynb

The notebook begins with boilerplate code that defines metadata for the
resulting files and also changes some defaults, such as the figure size and
resolution, font size, and legend frame. After that there are a few helpful
functions which I will discuss below.

### Draw Bands

One of my favorite functions is `draw_bands()`. It draws a set of alternating colored
bands on the background of the plot based on the axis tick locations.

When called with just the axis, like `draw_bands(ax)`, it produces this:

[![A plot showing the default grey bands.][bands_plot]][bands_plot]

[bands_plot]: /files/jupyter-library//bands.svg

But you can also customize the color using `draw_bands(ax, color="orange",
alpha=0.05)`, which produces:

[![A plot showing the orange bands.][orange_bands_plot]][orange_bands_plot]

[orange_bands_plot]: /files/jupyter-library//orange_bands.svg

These bands are a subtle way of indicating where on the X-axis a point lies,
which is especially useful when plotting a time series. I use them often. Here
are some examples:

- [**Discussing my sons' language development**][language_post] to highlight each month.

- [**Plotting the progression of the cycling hour record**][hour_post] to show each decade.

- [**Exploring when cyclists are involved in traffic accidents**][bike_post] to highlight the seasonality.

[language_post]: /blog/my-sons-language-development-comparison/#development
[hour_post]: /blog/hour-record-plot-improvements/#improvements
[bike_post]: /blog/switrs-bicycle-crashes-by-date/#day-by-day

### Draw Legends

I like minimal, but informative, legends. Color alone is often enough to
differentiate lines or points, so I wrote a function to change the color of
the legend text to match the line, called `draw_colored_legend()`. It produces
a legend like on this plot:

[![A plot showing my colored legend.][legend_plot]][legend_plot]

[legend_plot]: /files/jupyter-library//legend.svg

This legend style can be seen in these posts:

- [**Plotting my son's language development**][son_post] to label each language.

- [**Plotting Tour de France Prize Money**][tdf_post] to label the winner's prize compared to the total.

- [**Comparing Data Science Salaries by Gender**][salary_post] to differentiate the points for men and women.

[son_post]: /blog/my-second-sons-words/#the-words
[tdf_post]: /blog/tdf-prize-money-plot-improvements/#improvements
[salary_post]: /blog/data-science-salaries-by-gender/#by-region

## Putting It Together

The [plotting notebook][plotting_nb] enables you to make beautiful plots
quickly and easily. For example, this plot:

[![An example plot from the notebook library][example]][example]

[example]: /files/jupyter-library//example_plot.svg

Was produced by this short code snippet:

```python
fig, ax = setup_plot(
    title="Title",
    xlabel="X-axis",
    ylabel="Y-axis",
)

ax.scatter(np.random.rand(500)-0.65, np.random.rand(500), label="First dataset")
ax.scatter(np.random.rand(500)-0.35, np.random.rand(500), label="Second dataset")

draw_colored_legend(ax)

draw_bands(ax)

save_plot(fig, "/tmp/output.svg")
```

If the notebook template library is useful to you, be sure to let me know on
[Twitter][twit] or [Github][github]. Your feedback helps make the project
better for everyone!

[twit]: https://twitter.com/alex_gude/
[github]: https://github.com/agude/Jupyter-Notebook-Template-Library/issues

## Related Posts
- [SWITRS: Pedestrian Safety on Halloween](/blog/switrs-pedestrian-incidents-on-halloween/)
- [SWITRS: On What Days Do Drivers Hit Pedestrians?](/blog/switrs-pedestrian-incidents-by-date/)
- [Plotting the 2022 Tour de France](/blog/2022-tour-de-france-plot/)