pip install pywaffle
from pywaffle import Waffle
import squarify
import pandas as pd
import matplotlib.pyplot as plt
Car market analysis
Source of data: https://github.com/selva86/datasets/blob/master/mpg_ggplot2.csv
GSuite Text and Background Palette: https://yagisanatode.com/2019/08/06/google-apps-script-hexadecimal-color-codes-for-google-docs-sheets-and-slides-standart-palette/
df= pd.read_csv('c:/1/mpg_ggplot2.txt')
df.head()
df = df.groupby('class').size().reset_index(name='counts')
df
# Prepare Data
n_categories = df.shape[0]
colors = [plt.cm.YlGnBu(i/float(n_categories)) for i in range(n_categories)]
# Draw Plot and Decorate
fig = plt.figure(
FigureClass=Waffle,
plots={
'111': {
'values': df['counts'],
'labels': ["{0} ({1})".format(n[0], n[1]) for n in df[['class', 'counts']].itertuples()],
'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 12},
'title': {'label': 'Vehicles by Class', 'loc': 'center', 'fontsize':28}
},
},
rows=7,
colors=colors,
figsize=(16, 9)
)
n_categories
colors
Titanic disaster
We ought to find which passengers have chance to survive according to their affiliation to the established groups.
Source of data: https://www.kaggle.com/shivamp629/traincsv
df2 = pd.read_csv('c:/1/kaggletrain.csv')
df2.head(3)
dfTDK = df2.groupby('Pclass').size().reset_index(name='counts')
dfTDK
paltes: https://matplotlib.org/examples/color/colormaps_reference.html
cmaps = [(‘Perceptually Uniform Sequential’, [
‘viridis’, ‘plasma’, ‘inferno’, ‘magma’]),
(‘Sequential’, [
‘Greys’, ‘Purples’, ‘Blues’, ‘Greens’, ‘Oranges’, ‘Reds’,
‘YlOrBr’, ‘YlOrRd’, ‘OrRd’, ‘PuRd’, ‘RdPu’, ‘BuPu’,
‘GnBu’, ‘PuBu’, ‘YlGnBu’, ‘PuBuGn’, ‘BuGn’, ‘YlGn’]),
(‘Sequential (2)’, [
‘binary’, ‘gist_yarg’, ‘gist_gray’, ‘gray’, ‘bone’, ‘pink’,
‘spring’, ‘summer’, ‘autumn’, ‘winter’, ‘cool’, ‘Wistia’,
‘hot’, ‘afmhot’, ‘gist_heat’, ‘copper’]),
(‘Diverging’, [
‘PiYG’, ‘PRGn’, ‘BrBG’, ‘PuOr’, ‘RdGy’, ‘RdBu’,
‘RdYlBu’, ‘RdYlGn’, ‘Spectral’, ‘coolwarm’, ‘bwr’, ‘seismic’]),
(‘Qualitative’, [
‘Pastel1’, ‘Pastel2’, ‘Paired’, ‘Accent’,
‘Dark2’, ‘Set1’, ‘Set2’, ‘Set3’,
‘tab10’, ‘tab20’, ‘tab20b’, ‘tab20c’]),
(‘Miscellaneous’, [
‘flag’, ‘prism’, ‘ocean’, ‘gist_earth’, ‘terrain’, ‘gist_stern’,
‘gnuplot’, ‘gnuplot2’, ‘CMRmap’, ‘cubehelix’, ‘brg’, ‘hsv’,
‘gist_rainbow’, ‘rainbow’, ‘jet’, ‘nipy_spectral’, ‘gist_ncar’])]
# Prepare Data
n_categories = dfTDK.shape[0]
colors2 = [plt.cm.cubehelix(i/float(n_categories)) for i in range(n_categories)]
## paltes: https://matplotlib.org/examples/color/colormaps_reference.html
# Draw Plot and Decorate
fig = plt.figure(dpi=380, FigureClass=Waffle,
plots={
'111': {
'values': dfTDK['counts'],
'labels': ["{0} ({1})".format(n[0], n[1]) for n in dfTDK[['Pclass', 'counts']].itertuples()],
'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.0, 1), 'fontsize': 28},
'title': {'label': 'Structure of passengers population of Titanic', 'loc': 'center', 'fontsize':68,'alpha':0.5}
},
},
rows=10,
colors=colors2,
figsize=(28, 7)
)
Embarked: (C = Cherbourg, Q = Queenstown, S = Southampton)
df2.Embarked = df2.Embarked.str.replace('C', 'Cherbourg')
df2.Embarked = df2.Embarked.str.replace('Q', 'Queenstown')
df2.Embarked = df2.Embarked.str.replace('S', 'Southampton')
df2.sample(4)
dfPKP = df2.groupby('Embarked').size().reset_index(name='counts')
dfPKP
# Prepare Data
n_categories = dfPKP.shape[0]
colors2 = [plt.cm.bwr(i/float(n_categories)) for i in range(n_categories)]
## paltes: https://matplotlib.org/examples/color/colormaps_reference.html
# Draw Plot and Decorate
fig = plt.figure(dpi=380, FigureClass=Waffle,
plots={
'111': {
'values': dfPKP['counts'],
'labels': ["{0} ({1})".format(n[0], n[1]) for n in dfPKP[['Embarked', 'counts']].itertuples()],
'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.0, 1), 'fontsize': 28},
'title': {'label': 'Place of embarking of passengers on the Titanic', 'loc': 'center', 'fontsize':58,'alpha':0.5}
},
},
rows=10,
colors=colors2,
figsize=(28, 7)
)
df3 = pd.read_csv('c:/1/bank.csv')
df3.head(3)
df_STS = df3.pivot_table(index='job', values = 'Unnamed: 0',aggfunc='count').reset_index()
df_STS.sort_values('Unnamed: 0', ascending=False)
n_categories = df_STS.shape[0]
colors2 = [plt.cm.bwr(i/float(n_categories)) for i in range(n_categories)]
## paltes: https://matplotlib.org/examples/color/colormaps_reference.html
# Draw Plot and Decorate
fig = plt.figure(dpi=180, FigureClass=Waffle,
plots={
'111': {
'values': df_STS['Unnamed: 0'],
'labels': ["{0} ({1})".format(n[0], n[1]) for n in df_STS[['job', 'Unnamed: 0']].itertuples()],
'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.0, 1), 'fontsize': 128},
'title': {'label': 'Structure of bank customers by occupation', 'loc': 'center', 'fontsize':158,'alpha':0.5}
},
},
rows=150,
colors=colors2,
figsize=(118, 70)
)
Structure of airports
Source of data: http://ourairports.com/data/airports.csv
df4= pd.read_csv('c:/1/airports.csv')
df4.head(4)
PL = df4[df4['iso_country']=='PL']
PPL = PL.pivot_table(index='type', values='id',aggfunc = 'count').reset_index()
PPL
n_categories = PPL.shape[0]
colors2 = [plt.cm.RdYlBu(i/float(n_categories)) for i in range(n_categories)]
## paltes: https://matplotlib.org/examples/color/colormaps_reference.html
# Draw Plot and Decorate
fig = plt.figure(dpi=380, FigureClass=Waffle,
plots={
'111': {
'values': PPL['id'],
'labels': ["{0} ({1})".format(n[0], n[1]) for n in PPL[['type', 'id']].itertuples()],
'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.0, 1), 'fontsize': 28},
'title': {'label': 'Structure of airports in Poland', 'loc': 'center', 'fontsize':48,'alpha':0.8}
},
},
rows=10,
colors=colors2,
figsize=(28, 7)
)
