filmov
tv
Python Tutorial: Exploring and visualizing spatial data

Показать описание
---
In the previous video and exercises, we have seen the GeoDataFrame and its basic functionality.
A GeoDataFrame from the geopandas library is a spatially-enabled pandas DataFrame
Thus, everything you know about how to work with a pandas DataFrame can be used here as well, which makes that you can easily work with the attribute information of the geometries, to manipulate, explore and analyse them.
To give one example, taking a subset of the dataframe by filtering on one of the attributes.
Let's take the countries dataset again as example, a polygon dataset with all the countries of the world. If we look at the first rows, you see that there is a column indicating the continent.
So now we can do a comparison operation to look for all countries of the continent of Africa. This gives us a boolean Series with True and False values, also called a mask.
We can then use this boolean mask to filter the original GeoDataFrame.
Plotting the subset again, you can see we only have the countries of Africa.
This was one example of basic pandas functionality. In the exercises we will encounter some more, such as groupby and joining dataframes.
In the previous video and exercises, we have already seen the basic way to quickly plot the geometries in a GeoDataFrame: the plot method. In the remaining of this video, we will show some tricks to customize this plot and to make more advanced visualizations.
To start, we will see two ways to adjust the color of the plotted geometries.
First, we can specify a uniform color with the "color" keyword. For example, here we specify that all the countries should be plotted in red.
Alternatively, you often want to color each polygon depending on one of the attributes of those geometries.
For that, we can pass the column name of this attribute to the "column" keyword of the plot method. For example, here we color the countries based on its GDP per capita.
Notice that we also specified a figure size: the width and height in inches.
Next, we often want to combine multiple layers of geometries in a single plot.
For this, we can use the "ax" keyword of the plot method. With this keyword, we can pass an existing plot on which to add the additional layer.
It takes a matplotlib axes object, which is returned by the "plot" method, or which, as done here, can be created with the "subplots" function of matplotlib.
In this example, we create a figure with one subplot, and then plot the countries and the city locations on that subplot, each time specifying the ax keyword. Finally, we also use the "set_axis_off" method of matplotlib to remove the box with ticks and labels around the figure.
Finally, remember that the plot that is created is a matplotlib figure, similarly as for normal pandas dataframes. So the figure can also be further tweaked, such as setting the x and y limits or labels, changing the figure size, etc, using the matplotlib interface. We will encounter some examples in the coming exercises.
So let's practice.
#DataCamp #PythonTutorial #Geospatial #Data #Python #Exploring #visualizing #spatial #data