From the course: Advanced Spatial Data Visualization in Python

Vector data visualizations with Matplotlib - Python Tutorial

From the course: Advanced Spatial Data Visualization in Python

Vector data visualizations with Matplotlib

- [Instructor] We will start by collecting the right data. Here, we will again go back to the Natural Earth database. However, in contrast to the previous courses, now we are not going for a point-and-click downloading procedure, but downloading the datasets in an automatic way using Python. Now let's do a few imports and create our output directory. Then, let's grab the URL from the Natural Earth website. And finally, let's use this Python snippet to download the file and unzip it to the previously created data directory. Remember, whenever crawling data like this, we have to make sure we are compliant with the platform's policies and that we don't overload their server capacities with the number of requests. Once we have the data, let's import geopandas, read in the data table we just acquired, filter it down to a few slotted columns, and display it for a quick sanity check. Here, we can see that the data table has an admin column with the country names, a population and a GDP estimate column, and the geometry layer. Now let's create our first visual. This shouldn't be new. In fact, from our first course on, this should be a simple exercise. Here we use matplotlib and the built-in plot command of geopandas to draw a map, where each country is going to be colored based on its estimated GDP level, measured a million dollars. After the import, we create the canvas, which is empty at the moment, and now it is going to show this word map. So this map is quite all right, but it's not the best one. It lacks legends and labels and the most of it is green. So let's aim with a few more packages and start upgrading the map. The upgrade starts with the data. So let's do a few data preparation steps. First, let's exclude the Antarctica, since we are not expecting a major contribution of GDP there. Then we ensure that the GDP values are numeric and focus only on positive entries, since the low rate mix scaling which we are going to apply requires strictly positive numbers. To avoid extreme outliers dominating the map, we scale the data between the second and the 98th percentage using a log-based normalization. And finally, we reproject the geometries into the web market projection, so they can align with standard web-based maize easily, which is going to improve our visualization further. Next, we create a custom matplotlib canvas by setting the figure size first. Then we are going to make both the figure and the axis backgrounds transparent, and remove plot borders, thicks and thick labels. This gives us a clean and minimal base layer to our map visualization. As you can see here, we have a completely clean chemist. Now it's time to start visualizing our map. We are going to draw a coroplast map using the following set of comments. First, we specify the axis, so we are going to use the canvas we just has prepared. Then we'll select the GDP column to plot and use a yellow, green, blue color map. Then we are going to apply the normalization we prepared, and add some opacity to the plot. Then we will style the edge color of the polygons. Then we set the Z order to two to ensure that the polygons are shown on the top of the map. Then we switch on the legends and apply a series of formatting commands to make sure that we have decently formatted an easy to read legends. So we set the legend title. We set a shrinking perimeter telling us its size, positioning, and margin levels, and the exact location. And then we even format the way numbers are displayed and formatted on the labels. Then we run the command, and we have this quite decent looking map. So the map already looks cleaner with the customized coroplast map and glowing outlines, and the decent color bar as well. However, the plain white background feels a little off. So earlier we made the figure and the axis backgrounds transparent on purpose. This now allows us to introduce so-called base maps. Base maps are background map layers, such as satellite image maps, three maps, or terrain tiles, that provide geographic context. Instead of leaving the map on a plain background or filling it with one single color, we can overlay our thematic data on top of real world base maps. We will use the Contextual AI library to bring in these base maps, which automatically fetches the tiles and fill the background. We can use the providers available by simply querying the directory of the providers. And if we pick one provider, for example, CartoDB, then we can also quickly see all the available base maps. Now let's go back to the previous map. I will just copy and paste the code here and add one single line of code using the Contextual AI library and base map. Here again, we specify the axis. We pick one base map of our choice, and then we set the Z order to be zero. So the base map should certainly be in the background. After running the command, which might take a few seconds since here we are query an online map tile service, we will have this nice, properly drawn map. And with that, we are almost ready. We just have to fine tune the map's presentation by sty the title, recoloring texts, and finalizing the dark background coloring. So to do that I will add the following block of code, which adds a boldfaced white colored title to the image, which recolor the texts on the color bar to white. Then with the plt.tight_layout command sets the image in a nicer, much tighter constellation. And here since, we set the face color to none, we already get an empty black background. However, we can also set the background to be black by simply calling the patch set face color command and color it black. And now there is just one step left in case we would like to use this map in, for example, a report or a presentation. It is to save it using the plt.safety command, where we need to specify the name of the file, the resolution in dots per inch. And my recommendation is to set the bbox_inches perimeter to be tight. And having said that, we arrived to a nicely designed, unique vector map, based on geopanda and matplotlib, which shows the world map coloring each country by GDP, and setting literally all possible perimeters of this plotting workflow. Now, let's move on and see how we can do similar visuals with roster data.

Contents