|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Plotting with Bokeh" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "In this exercise we want to use the higher-level interface which is focused around providing a simple interface for quick visualization creation. \n", |
| 15 | + "Refer back to the topic about Bokeh in lesson 3 to check back with what we learned about the different interfaces of Bokeh.\n", |
| 16 | + "\n", |
| 17 | + "We will use the plotting interface to get some insights into the population density development of Germany and Switzerland. \n", |
| 18 | + "We want to check whether the statement of \"continous growth of density\" from lesson 1 is true by visualizing each data point for a specific country.\n", |
| 19 | + "\n", |
| 20 | + "We are already familiar with this exercises dataset since we've used it before in lesson 1. \n", |
| 21 | + "As a reminder, the world population dataset contains information about the population density of each country for different years." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "#### Loading our dataset" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 1, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "# importing the necessary dependencies\n" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 2, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "# make bokeh display figures inside the notebook\n" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "**Note:** \n", |
| 54 | + "The cell above allows us to plot the bokeh visualizations inline in the notebook. By default it will open a new tab in your browser window with the plot." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 3, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "# loading the Dataset with geoplotlib\n" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 4, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "# looking at the dataset\n" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "---" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "#### Plotting the first data" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "Before we are able to plot the line that displays the population density growth of Germany, we need to do some data extraction. \n", |
| 94 | + "In this case we only want to have the column headers that are years in order to later retrieve the values for those years from our dataset for the given country. \n", |
| 95 | + "When looking at our dataset, we can see that only the columns that are years start with a numerical character. We can use this knowledge in order to write a filter condition using list comprehension. \n", |
| 96 | + "\n", |
| 97 | + "Once we have the years extracted, we can use this list of years to retrieve the values for given years from our country row of the dataset using the `loc` method which we got to know in the first lesson. \n", |
| 98 | + "Again, we will use a list comprehension to extract that kind of knowledge from our dataframe." |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 5, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# preparing our data for Germany\n" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "After preparing the data to be plotted, we can set up our visualization by first defining a plot using the `figure` method. \n", |
| 115 | + "This is the place where we can define the `title` and the text for the `x` and `y` axis of our plot. \n", |
| 116 | + "\n", |
| 117 | + "Once we have defined our plot figure, we can then add \"*layers*\" onto that plot. \n", |
| 118 | + "There are several pre-defined visualizations incorporated. \n", |
| 119 | + "- Lines\n", |
| 120 | + "- Scatters\n", |
| 121 | + "- Bars\n", |
| 122 | + "- Patches\n", |
| 123 | + "\n", |
| 124 | + "> You can find even more in the official documentaion: https://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#userguide-plotting\n", |
| 125 | + "\n", |
| 126 | + "\n", |
| 127 | + "Comparable to Matplotlib, the final call to display is done with the `show` method. \n", |
| 128 | + "By default, this will open a new browser window/tab displaying the visualization. However since we're using `output_notebook` here, it will be embeded into this notebook." |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 6, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "# plotting the population density change in Germany in the given years\n" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "**Note:** \n", |
| 145 | + "Note that the amount of data points in the first and second argument passed to the plotting methods has to be the same. \n", |
| 146 | + "If your `x` list has 10 values, your `y` list also has to have 10 values." |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "---" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "#### Simple plotting with geoplotlib" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "metadata": {}, |
| 166 | + "source": [ |
| 167 | + "When using bokehs high-level plotting interface, we are able to display several graphs in one plot. \n", |
| 168 | + "This means that we can simply add different layers on top of each other to create more comlex constructs. \n", |
| 169 | + "In our case, we want to add the population denstiy development of Switzerland on top of the one from Germany. \n", |
| 170 | + "We also want to visually distinguish them from each other by using different colors and also adding a different graph style to our Switzerland line. \n", |
| 171 | + "\n", |
| 172 | + "We first need to filter our data to extract the data of Switzerland. \n", |
| 173 | + "After we've done that, we can use the same technique we saw in the first task to plot the line." |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 7, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "# preparing the data for the second country\n" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": 8, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "# plotting the data for Germany and Switzerland in one visualization, \n", |
| 192 | + "# adding circles for each data point for Switzerland\n" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "**Note:** \n", |
| 200 | + "Using Bokeh, we are also able to stack different graphs on top of each other which gives us the tooling to not only differentiate the graphs by color but other features like different line styles." |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "markdown", |
| 205 | + "metadata": {}, |
| 206 | + "source": [ |
| 207 | + "In the next task, we want to display the plots next to each other instead if having them stacked. \n", |
| 208 | + "In some cases this will allow us to compare many plots better since they are not layered on top of each other. \n", |
| 209 | + "We can still make sure all of the plots have the same `x` and `y` axis for better comparison by providing the `x_range` and `y_range` values to our other figures. \n", |
| 210 | + "\n", |
| 211 | + "Bokeh provides a submodule that gives us access to several layouts. So we need an additional import here. \n", |
| 212 | + "The `gridplot` object allows us to provide a two dimensional array which describes the positions of the plots on a grid." |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "code", |
| 217 | + "execution_count": 9, |
| 218 | + "metadata": {}, |
| 219 | + "outputs": [], |
| 220 | + "source": [ |
| 221 | + "# plotting the Germany and Switzerland plot in two different visualizations\n", |
| 222 | + "# that are interconnected in terms of view port\n" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": {}, |
| 228 | + "source": [ |
| 229 | + "**Note:** \n", |
| 230 | + "This means that if we provide two lists nested, we can control the output to be printed in a vertically stacked way instead of next to each other." |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "code", |
| 235 | + "execution_count": 10, |
| 236 | + "metadata": {}, |
| 237 | + "outputs": [], |
| 238 | + "source": [ |
| 239 | + "# plotting the above declared figures in a vertical manner\n" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "markdown", |
| 244 | + "metadata": {}, |
| 245 | + "source": [ |
| 246 | + "We can see that as soon we have our data prepared, it's fairly simple to visualize the data in a simple plot. \n", |
| 247 | + "Using the `bokeh.plotting` interface allows us to display the data with different, so called, `glyphs` without much work. " |
| 248 | + ] |
| 249 | + } |
| 250 | + ], |
| 251 | + "metadata": { |
| 252 | + "kernelspec": { |
| 253 | + "display_name": "Python 3", |
| 254 | + "language": "python", |
| 255 | + "name": "python3" |
| 256 | + }, |
| 257 | + "language_info": { |
| 258 | + "codemirror_mode": { |
| 259 | + "name": "ipython", |
| 260 | + "version": 3 |
| 261 | + }, |
| 262 | + "file_extension": ".py", |
| 263 | + "mimetype": "text/x-python", |
| 264 | + "name": "python", |
| 265 | + "nbconvert_exporter": "python", |
| 266 | + "pygments_lexer": "ipython3", |
| 267 | + "version": "3.7.0" |
| 268 | + } |
| 269 | + }, |
| 270 | + "nbformat": 4, |
| 271 | + "nbformat_minor": 2 |
| 272 | +} |
0 commit comments