EDA: Visualization in Geopandas, Matplotlib & Bokeh | by Denise Chen | Towards Data Science

Nowadays, everyone is immersed with plenty of data from news sources, cellphones, laptops, workplaces, and so on. Data conveys with tons of information from different data variables like date, string, numeric, and geographical format. How to effectively grasp the core value from a huge dataset that is easily interpreted by users? The answer would be the Exploratory Data Analysis (EDA). EDA comes as a tool to visualize and analyze data to extract insights from the dataset. Viewers are able to have a better understanding of the dataset from the important characteristics summarized through the process of EDA.
In this article, you will learn:
(1) Dynamic geographical plot with Geopandas and Bokeh
(2) Analytics on worldwide dataset from 2016 to 2019
(3) Visualization in Matplotlib and Bokeh
Choropleth map provides various patterns and symbols on geographic areas (i.e. countries) which shows a good representation of measurement across regions. To create a global choropleth map, we’ll focus on the survey of the state of global happiness, which ranks 155 countries by their happiness levels, released at the United Nations. Links to the Kaggle website: World Happiness Report. The creation of the plot is using Python libraries and packages — Pandas, Geopandas and Bokeh.
To render a world map, it is needed to have a shapefile with world coordinates. Natural Earth is a great source to download geospatial data, filled with various public domain map dataset. For the generation of dynamic geographical plot, 1–110m small scale data comes as a good map dataset.
Geopandas can convert ESRI shapefile into a GeoDataframe object with read_file function. Geopandas can read almost any vector-based spatial data format including ESRI shapefile using read_file command which returns a GeoDataframe object. You can specify the columns while reading the dataset with geopands function.
First, we create a data frame of the world happiness report and specify the year of 2015. The resulting data frame df_2015 can then be merged to the GeoDataframe gdf. For later use of Bokeh to create the visualization, we need to have geojson format data for the source of plotting. A collection of features contains points, lines, and polygons from GeoJSON data. Therefore, we convert the data frame into JSON and converts it to string-like object.
The merged file is a GeoDataframe object that can be rendered using geopandas module. However, since we want to incorporate data visualization interactivity, we will use the Bokeh library. Bokeh consumes GeoJSON format which represents geographical features with JSON. GeoJSON describes points, lines, and polygons (called Patches in Bokeh) as a collection of features. We therefore convert the merged file to the GeoJSON format.
Then, we are ready to create a static choropleth map with the Bokeh module. We first read in geojson data withGeoJSONDataSource package. Next, we assign a color palette as ‘YlGnBu’ and reverse the color order to match the darkest color for the highest happiness score. Then, we apply custom tick labels for color bars. For the color bar, we map the color mapper, orientation, and tick labels into the ColorBar package.
We create the figure object with the assignment of plot height and width. Then, we add patches for the figure with x and y coordinates, and specify the field and transform columns in the fill_colors parameter. To display the bokeh plot in the Jupyter notebook, we need to put the output_notebook() module and have the figure displayed in the show() module.
From the plot below, we see that countries like Canada, Mexico, and Australia have a higher happiness score. For South America, and European countries, the overall score is distributed around Index 5 and 6. In Contrast, African countries like Niger, Chad, Mali, and Benin show a much lower happiness index.
There are two parts added for the interactive choropleth map. One is the creation of a hover tool. We assign the columns for the information displayed on the graph. The other is the creation of the callback function. For the plot interaction, we specify the year through the slider to update the data. We pass the slider value to the callback and have the data adjusted. Then, we pass the slider object to the widgetbox parameter in the bokeh Column class. Finally, we add the curdoc class to create interactive web applications that can connect front-end UI events to real, running Python code.
For those who have error to run the choropleth map in the Jupyter notebook, there’s an alternative to run the script in the terminal.
bokeh serve --show EDA_Plot.py
We look into the correlation of GDP Growth and happiness levels score in 2016. As the countries are color-coded by regions, we can see that southeast countries have lower GDP growth followed by underlying happiness scores. Most countries in central and eastern Europe have GDP growth fall within 0.8 and 1.4 with a happiness score between 5 and 6. For the region of Western Europe, they tend to show a higher range of economic growth along with the happiness index.
For the top 10 economy trend countries, ‘United Arab Emirate’ has shown the increasing trend with 0.68 growth on the economy from 2015 to 2018. ‘Myanmar’ has a rising rate with 0.41 on GDP per Capita growth as one only Asian country. Surprisingly, Sub-Saharan Africa countries like ‘Malawi’, ‘Guinea’, ‘Tanzania’ are the top 5 countries with the upward economic trend.
We can see that countries with decreased economic trends are mostly in Africa. Bottom 5 countries like ‘Libya’, ‘Yemen’, ‘Kuwait’, ‘Jordan’, ‘Sierra Leone’ have lower Economy Index from 2015 to 2018. Four of those countries are located in the Middle East and Northern Africa.
Seeing the top and Bottom 10 Countries of Economy Index (GDP per capita growth), we closely look into the United Arab Emirate’s economic trend. In 1980, UAE shows the max GDP growth value among 40 years. However, the growth becomes negative in the range of the year 1982 to 1986. In the next 10 years, UAE shows a quite stable GDP growth around 0.1 to 0.2 rise. In the year 2009, there’s a plunge on GDP growth followed by the impact of the financial crisis.
To create a choropleth map, geopands can convert shp files into the data frame object. For the creation of visualization, bokeh works well with the geopandas package. However, it’s better to mind that countries need to be matched from the ship file with the outsource data when merging both datasets.
Matplotlib and Bokeh are two great packages for visualization tool in Python. Scatter plot better shows the correlation of 2 variables with numeric values. In terms of the diverging plot, it better shows the downward and upward trend of the dataset. For the DateTime format variable, it’s better to take care of date with a missing value for the plot creation. The line graph displays a distinct trend on the time series data.