{ "cells": [ { "cell_type": "markdown", "id": "54768bc4-9375-426b-9a99-0c7ccd4d0b75", "metadata": {}, "source": [ "# Effective plot design: line plots\n", "\n", "As we have seen earlier in this chapter, you should be aiming to produce plots that include all of the elements that help make understanding the plotted data intuitive. Typically, this might include:\n", "\n", "- Axis labels, including units if needed\n", "- A legend\n", "- Grid lines\n", "- Plot and/or figure titles\n", "- Annotation on the plot, such as text\n", "\n", "In addition, there are several factors that can help improve the communication of the plotted information. When plotting line data, for example, the color of the lines might be an important consideration to tell different lines apart. This is especially true when plotting several lines on the same axes, as readers will need to be able to differentiate the lines and know which line refers to a given set of data. But there is more to plotting lines than simply choosing nice colors. \n", "\n", "Not all people viewing your plots will see them the same way. Some viewers may have color blindness, while others may have printed out a copy of your plot in grayscale from a printer. Thus, while choosing nice colors can help make your plots look visually pleasing to you, it is worthwhile to consider the other viewers or formats in which your plots may be viewed. In this way your visualizations can be as inclusive to different viewers as possible." ] }, { "cell_type": "markdown", "id": "3749b3e0-71cf-413a-9c88-a8de8615fc0f", "metadata": {}, "source": [ "## Tips for plotting lines\n", "\n", "Let's consider an illustrative example. In this case we will use four lines to plot hypothetical monthly temperatures for various mythical lands in the year 1680 [^nerds]. We will use a pandas DataFrame called `data` for this purpose with four columns and one year of data. We can see temperatures for the first four months in the data table below by typing `data.head(4)`." ] }, { "cell_type": "code", "execution_count": 1, "id": "a4801d4b-d318-44a7-b09c-6127b1465d1d", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "# Load the libraries we need\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "dates = pd.date_range(start=\"16800101\", end=\"16801201\", freq=\"MS\")\n", "temperatures = {\n", " \"Asgard\": [\n", " 2.61080759,\n", " 3.80300147,\n", " 6.81951259,\n", " 8.07302111,\n", " 10.92665915,\n", " 13.18569725,\n", " 11.15190422,\n", " 11.88095271,\n", " 8.16883214,\n", " 7.27447255,\n", " 3.43161114,\n", " 2.78345483,\n", " ],\n", " \"Camelot\": [\n", " 6.91054909,\n", " 7.78720085,\n", " 7.69825447,\n", " 12.35036454,\n", " 14.84349615,\n", " 19.77590178,\n", " 19.3768641,\n", " 17.37775864,\n", " 11.06999359,\n", " 9.30251052,\n", " 7.4073777,\n", " 6.61132001,\n", " ],\n", " \"Nysa\": [\n", " 9.97005829,\n", " 13.20188993,\n", " 12.94964658,\n", " 16.57315997,\n", " 21.00721179,\n", " 22.90791358,\n", " 22.53282688,\n", " 19.92502575,\n", " 19.95551156,\n", " 15.53906563,\n", " 10.7195169,\n", " 10.19603786,\n", " ],\n", " \"Paititi\": [\n", " 7.73300265,\n", " 9.85720691,\n", " 18.96796882,\n", " 20.39733145,\n", " 30.67161633,\n", " 35.05950444,\n", " 29.18180578,\n", " 31.1744113,\n", " 16.49727756,\n", " 14.0604099,\n", " 9.07097188,\n", " 9.36868944,\n", " ],\n", "}\n", "\n", "data = pd.DataFrame(index=dates, data=temperatures)" ] }, { "cell_type": "code", "execution_count": 2, "id": "478a2a8b-9c62-4730-a36d-df91803a5342", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Asgard | \n", "Camelot | \n", "Nysa | \n", "Paititi | \n", "
---|---|---|---|---|
1680-01-01 | \n", "2.610808 | \n", "6.910549 | \n", "9.970058 | \n", "7.733003 | \n", "
1680-02-01 | \n", "3.803001 | \n", "7.787201 | \n", "13.201890 | \n", "9.857207 | \n", "
1680-03-01 | \n", "6.819513 | \n", "7.698254 | \n", "12.949647 | \n", "18.967969 | \n", "
1680-04-01 | \n", "8.073021 | \n", "12.350365 | \n", "16.573160 | \n", "20.397331 | \n", "