{ "cells": [ { "cell_type": "markdown", "id": "bb871819-7798-46bb-b6bb-c488e1a1fdd3", "metadata": {}, "source": [ "# Plotting in Python\n", "\n", "Data visualization is an essential part of understanding and interpreting data, and Python has a large number of libraries available for use in visualizing different types of data. Below we provide a bried overview of some of the Python plotting landscape as well as an introduction to the terminology commonly used to refer to different parts of a plot created using Python." ] }, { "cell_type": "markdown", "id": "9e5c1983-3184-4c38-8386-7568f8a21f8e", "metadata": {}, "source": [ "## Python plotting libraries\n", "\n", "![_**Figure 4.1**. Plotting libraries available in Python. Interactive version online at ._](../img/python-plotting.png)\n", "\n", "_**Figure 4.1**. Plotting libraries available in Python. Interactive version online at ._\n", "\n", "Python has many nice, useful libraries that can be used for plotting. In the figure above, you can see a number of the available plotting library options, along with how they relate to one another. Of the options above we would like to highlight the following:\n", "\n", "- [Matplotlib](https://matplotlib.org/) [^matplotlib]: Matplotlib is one of the most widely used Python plotting libraries, sometimes referred to as \"*the grand old man of Python plotting*\". Plot examples can be found in the [Matplotlib gallery](https://matplotlib.org/gallery.html).\n", "\n", " - [Matplotlib Basemap](https://matplotlib.org/basemap/index.html) [^basemap]: The Matplotlib Basemap Toolkit is a plugin for visualizing maps in Python. Example plots available in the [Matplotlib basemap gallery](https://matplotlib.org/basemap/users/examples.html).\n", " - [Seaborn](https://seaborn.pydata.org/) [^seaborn]: Seaborn is a high-level interface for drawing attractive statistical graphics that is built on top of Matplotlib. Example plots can be found in the [Seaborn gallery](https://seaborn.pydata.org/examples/index.html).\n", "\n", "- [Bokeh](https://docs.bokeh.org/en/latest/) [^bokeh]: Bokeh is a modern plotting library for static and interactive web-based plots including graphs, maps, and charts. Examples can be found in the [Bokeh gallery](https://docs.bokeh.org/en/latest/docs/gallery.html).\n", "- [Plotly](https://plotly.com/python/) [^plotly]: Similar in some ways to Bokeh, Plotly is a modern plotting library for static and interactive web-based plots. Some features are commercial. Example plots are available in the [Plotly gallery](https://plotly.com/python/basic-charts/).\n", "- [Dash](https://plotly.com/dash/) [^dash]: Dash is a Python framework for building analytical web applications. No JavaScript required.\n", "- [ggplot](https://yhat.github.io/ggpy/) [^ggplot]: ggplot is a Python plotting environment for those familiar with creating plots in R using ggplot2. You can use ggplot in Python too! Plot examples can be found in the [ggplot examples](https://yhat.github.io/ggpy/).\n", "- [HoloViews](https://holoviews.org/) [^holoviews]: and [GeoViews](https://geoviews.org/) [^geoviews]: HoloViews and GeoViews aim to let the data visualize itself. Learn more in the HoloViews introductory video at .\n", "\n", " - Both are modern and powerful visualization libraries built on top of Matplotlib and Bokeh that make exploring and visualizing your data quicker than ever before.\n", " - HoloViews is designed for basic plotting. More information available in the [HoloViews tutorial](https://holoviews.org/Tutorials/index.html) and the [HoloViews example plots](https://holoviews.org/Examples/index.html).\n", " - GeoViews is designed for creating nice and interactive maps. Examples can be found in the [GeoViews gallery](https://geoviews.org/gallery/index.html).\n", "\n", "You should explore the plotting galleries and examples of different visualization libraries above to learn what's possible to do in Python. As you will see, the plotting possibilities in Python are numerous and rich. To get started, we suggest starting by learning to use one that suits your needs best, and later extending your knowledge and skills to other visualization libraries as necessary." ] }, { "cell_type": "markdown", "id": "bf79e251-0e37-44ab-8eb6-bbfa242a5b1a", "metadata": {}, "source": [ "## Anatomy of a plot\n", "\n", "There are a variety of different kinds of plots (also known as graphs, charts, diagrams, etc.) available that have been designed to visually represent the characteristics of a dataset. Here is a list of several different types of plots that can be used to present different kinds of data. You can find more information about this plots online in Wikipedia, for example.\n", "\n", "- [Bar chart](https://en.wikipedia.org/wiki/Bar_chart)\n", "- [Histogram](https://en.wikipedia.org/wiki/Histogram)\n", "- [Scatter plot](https://en.wikipedia.org/wiki/Scatter_plot)\n", "- [Line chart](https://en.wikipedia.org/wiki/Line_chart)\n", "- [Scatter plot](https://en.wikipedia.org/wiki/Scatter_plot)\n", "- [Pie chart](https://en.wikipedia.org/wiki/Pie_chart)\n", "- [Box plot](https://en.wikipedia.org/wiki/Box_plot)\n", "- [Violin plot](https://en.wikipedia.org/wiki/Violin_plot)\n", "- [Dendrogram](https://en.wikipedia.org/wiki/Dendrogram)\n", "- [Chord diagram](https://en.wikipedia.org/wiki/Chord_diagram)\n", "- [Treemap](https://en.wikipedia.org/wiki/Treemap)\n", "- [Network chart](https://en.wikipedia.org/wiki/Network_chart)\n", "\n", "However, before starting to visualize our data on a plot our data we need to address an obvious question: **What actually is a plot?** We will not go deep into the details of different types of plots such as those listed above, as it is not the purpose of this book, but rather we will provide a brief introduction to different plots that can be created using Python and the (essential) elements of a plot.\n", "\n", "![_**Figure 4.2**. The basic elements of a plot. Image source: Tenkanen (2017)._](../img/basic-elements-of-plot.png)\n", "\n", "_**Figure 4.2**. The basic elements of a plot. Image source: Tenkanen (2017)._\n", "\n", "\n", "In spite of the large variety of types of plots, there are certain elements that are common for most of them (not all). Thus, it is useful to know at least the basic terminology since it makes it easier to find help and information from the internet when you start creating or modifying your own plots. Figure 4.2 illustrates different elements of a basic line plot." ] }, { "cell_type": "markdown", "id": "b432e853-d6a2-48b0-80d4-2cbbe940966d", "metadata": {}, "source": [ "### Common plotting terminology\n", "\n", "The common terms in Table 4.1 may vary a bit depending on the plotting library that you use. For the list provided here we are using the typical terms for plotting in Matplotlib (see also Figure 4.2).\n", "\n", ": _**Table 4.1**. Common terminology for plot features in Matplotlib._\n", "\n", "| Term | Description |\n", "|:-----------|:--------------------------------------------------|\n", "| Axis | Axis of the graph that are typically x, y and z |\n", "| | (for 3D plots). |\n", "| Title | Title of the whole plot. |\n", "| Label | Name for the whole axis (e.g. xlabel or ylabel). |\n", "| Legend | Legend for the plot. |\n", "| Tick label | Text or values that are represented on the axis. |\n", "| Symbol | Symbol for data point(s) (on a scatter plot) that |\n", "| | can be presented with different symbol shapes or |\n", "| | colors. |\n", "| Size | Size of, for example, a point on a scatter plot. |\n", "| | Also used for referring to text sizes on a plot. |\n", "| Linestyle | The style how the line should be drawn. Can be |\n", "| | solid or dashed, for example. |\n", "| Linewidth | The width of a line in a plot. |\n", "| Alpha | Transparency level of a filled element in a plot |\n", "| | (values range between 0.0 (fully transparent) to |\n", "| | 1.0 (no trasnparency)). |\n", "| Tick(s) | Refers to the tick marks on a plot. |\n", "| Annotation | Refers to the text added to a plot. |\n", "| Padding | The distance between a (axis/tick) label and the |\n", "| | axis. |\n" ] }, { "cell_type": "markdown", "id": "88492595-c49f-429f-9c4f-7ede5ac94ab8", "metadata": {}, "source": [ "## Footnotes\n", "\n", "[^basemap]: \n", "[^bokeh]: \n", "[^dash]: \n", "[^geoviews]: \n", "[^ggplot]: \n", "[^holoviews]: \n", "[^matplotlib]: \n", "[^plotly]: \n", "[^seaborn]: " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }