Beautiful Bubble Plots in Matplotlib

Beautiful Bubble Plots in Matplotlib

Data Preparation

import numpy as np  
import pandas as pd
df = pd.read_excel('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx',
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
df.columns#Output:
Index([ 'Type', 'Coverage', 'OdName', 'AREA', 'AreaName', 'REG',
'RegName', 'DEV', 'DevName', 1980, 1981, 1982,
1983, 1984, 1985, 1986, 1987, 1988,
1989, 1990, 1991, 1992, 1993, 1994,
1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012,
2013],
dtype='object')
df = df.drop(columns = ['Type', 'Coverage', 'AREA', 'AreaName',      'REG', 'RegName', 'DEV', 'DevName',]).set_index('OdName')
df.head()
Image for post
Ireland = df.loc['Ireland']
Brazil = df.loc['Brazil']

Normalize the Data

i_normal = Ireland / Ireland.max()
b_normal = Brazil / Brazil.max()
years = list(range(1980, 2014))

Make the Bubble Plot

import matplotlib.pyplot as plt
plt.figure(figsize=(14, 8))
plt.scatter(years, Ireland, color='blue')
plt.scatter(years, Brazil, color='orange')
plt.xlabel("Years", size=14)
plt.ylabel("Number of immigrants", size=14)
plt.show()
Image for post
plt.figure(figsize=(12, 8))
plt.scatter(years, Brazil,
color='darkblue',
alpha=0.5,
s = b_normal * 2000)
plt.scatter(years, Ireland,
color='purple',
alpha=0.5,
s = i_normal * 2000,
)
plt.xlabel("Years", size=14)
plt.ylabel("Number of immigrants", size=14)
Image for post
c_br = sorted(Brazil)
c_fr = sorted(France)
plt.figure(figsize=(12, 8))
plt.scatter(years, Brazil,
c=c_br,
alpha=0.5,
s = b_normal * 2000)
plt.scatter(years, Ireland,
c=c_fr,
alpha=0.5,
s = i_normal * 2000,
)
plt.xlabel("Years", size=14)
plt.ylabel("Number of immigrants", size=14)
Image for post
plt.figure(figsize=(12, 8))
plt.scatter(years, Brazil,
c=c_br,
alpha=0.5,
s = b_normal * 2000)
plt.xlabel("Years", size=14)
plt.ylabel("Number of immigrants of Brazil", size=14)
Image for post

 

#matplolib #dataVizualization #bubbleplot #dataScience #DataAnalysis #python #plots

Leave a Reply

Close Menu