This represents my attempt to makeover a graph using seaborn. The project idea comes from who provide weekly datasets and visualations to makeover. The community is centered around visualizations in Tableau but my attempt below uses Seaborn.

Original: article

Data: xls

Load data

In [1]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
df = pd.read_excel('sustainable mobility.xlsx')

City Sustainable Cities Mobility Index
0 Hong Kong 0.653
1 Zurich 0.650
2 Paris 0.645
3 Seoul 0.644
4 Prague 0.643

Create Country feature

In [3]:
from geopy import geocoders

gn = geocoders.GoogleV3(timeout=5)
In [4]:
df = df[~df['City'].str.contains('Undisclosed')] #Drop undisclosed cities
In [5]:
In [6]:
gn.geocode("Hong Kong")[0].split(',')[-1]
'Hong Kong'
In [7]:
df['Country'] = df['City'].apply(lambda city: gn.geocode(city)[0].split(',')[-1].strip())

Final submission

In [8]:

Top 15 Cities

In [9]:
sns_plot = plt.figure(figsize=(12,7))
sns.stripplot(x='Sustainable Cities Mobility Index',y='City', size=10,data=df.head(20),palette="Reds_r")
plt.title('Hong Kong Leads the Way in Sustainable Transport')
sns_plot.savefig("Sustainable Transport.png");

Runner up: City scores grouped by country

In [10]:
top_countries = df.groupby('Country').mean().sort_values(by='Sustainable Cities Mobility Index',ascending=False).head(20).index

top_countries_df = df[df['Country'].isin(top_countries)]

sns.stripplot(x='Sustainable Cities Mobility Index',y='Country', size=8,data=top_countries_df,palette="Reds_r", edgecolor="gray")
sns.despine(left=True, bottom=True)

Previous iterations and graphs

Simple bar chart of top 10 cities

In [11]:
sns.barplot(y='City',x='Sustainable Cities Mobility Index',data=df.head(10))
plt.title("Cities with Highest Mobility Index")
plt.xlabel('Sustainable Cities Mobility Index');

Strip plot of all cities

In [12]:
sns.stripplot(x='Sustainable Cities Mobility Index',y='Country', size=8,data=df,palette="Reds_r", edgecolor="gray")
sns.despine(left=True, bottom=True)

Box plot of countries and mobility index

In [13]:
sns.boxplot(x='Country',y='Sustainable Cities Mobility Index',data=df)
<matplotlib.axes._subplots.AxesSubplot at 0x2649c2dec18>

Top 10 cities with country legend

In [14]:
sns.stripplot(x='Sustainable Cities Mobility Index',y='City', hue='Country',size=10,data=df.head(10), edgecolor="gray")
sns.despine(left=True, bottom=True)
Categories: Seaborn

Leave a Reply

Your email address will not be published. Required fields are marked *