In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
In [2]:
# Prepare Data
df = pd.read_csv('c:/1/mpg_ggplot2.txt')
df2 = df.pivot_table(index='manufacturer',values='model', aggfunc='count').reset_index()
df2 = df2.rename(columns={'model':'counts'})
# bardzo ważne żeby wykres był ładnie posortowany
df2.sort_values('counts', ascending=False, inplace=True)
df2.reset_index(inplace=True)
df2
Out[2]:
In [3]:
df.head(3)
Out[3]:
In [4]:
KAT = df.pivot_table(index =['hwy', 'cty'], values ='model', aggfunc='count' ).reset_index()
KAT = KAT.rename(columns ={'model':'counts'})
KAT.head()
Out[4]:
Stripplot
Size of circle is bigger as more points overlap.
In [26]:
import seaborn as sns
# Draw Stripplot
fig, ax = plt.subplots(figsize=(8,6), dpi= 280)
sns.stripplot(KAT.cty, KAT.hwy, size=KAT.counts*2, ax=ax)
# Decorations
plt.title('Counts Plot - Size of circle is bigger as more points overlap', fontsize=12)
plt.show()
Plot Bars
In [6]:
# Plot Bars
plt.figure(figsize=(10,4), dpi= 280)
plt.bar(df2['manufacturer'], df2['counts'], color=['#7f6000','#bf9000','#f1c232','#ffd966','#ffe599','#fff2cc'], alpha=0.4, width=.5)
for i, val in enumerate(df2['counts'].values):
plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':10})
# Decoration
plt.gca().set_xticklabels(df2['manufacturer'], rotation=90, horizontalalignment= 'right')
plt.title("Number of Vehicles by Manaufacturers", fontsize=14)
plt.ylabel('Vehicles')
plt.ylim(0, 45)
plt.show()
Trigger
Dane do wykresu
x1 opis
x2 dane liczbowe
In [7]:
name = df2.manufacturer
x = df2.counts
In [8]:
ylabel = 'Vehicles'
title = 'Number of Vehicles by Manaufacturers'
In [9]:
def bar1(name, x, ylabel, title):
# Plot Bars
plt.figure(figsize=(10,4), dpi= 280)
plt.bar(name, x, color=['#0c343d','#134f5c','#45818e','#76a5af','#a2c4c9','#d0e0e3'], alpha=0.4, width=.5)
for i, val in enumerate(df2['counts'].values):
plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':10})
# Decoration
plt.gca().set_xticklabels(df2['manufacturer'], rotation=90, horizontalalignment= 'right')
plt.title(title, fontsize=14)
plt.ylabel(ylabel)
plt.ylim(0, 45)
plt.show()
In [10]:
bar1(name, x, ylabel, title)
colors = ['#274e13','#6aa84f','#93c47d', '#b6d7a8','#d9ead3','#b7b7b7','#38761d'] #green
colors = ['#0c343d','#134f5c','#45818e','#76a5af','#a2c4c9','#d0e0e3'] #cyan colors = ['#7f6000','#bf9000','#f1c232','#ffd966','#ffe599','#fff2cc'] #yelowcolors = ['#4c1130','#a64d79','#c27ba0','#d5a6bd','#ead1dc','#741b47',] #magentacolors = ['#e6b8af','#b6d7a8','#e06666','#747574','#ffd966','#ffcc99','#ea9999'] colors = ['#93c47d','#b6d7a8','#d9ead3','#d0e0e3','#a2c4c9','#76a5af']
colors = ['#c27ba0','#d5a6bd','#ead1dc','#ffffff','#a64d79','#d9d2e9','#b4a7d6'] #purple colors = ['#cfe2f3','#9fc5e8','#6fa8dc'] #blue colors = ['#d9ead3','#b6d7a8','#93c47d','#6aa84f'] colors = ['#ff0000','#434343','#666666','#999999','#b7b7b7','#cccccc','#d9d9d9','#efefef','#ffffff','#f3f3f3'] #=> niemieckie czasopismo
In [11]:
df3 = pd.read_csv('c:/1/airports.csv')
df3.head(3)
Out[11]:
In [12]:
PPS = df3.pivot_table(index = 'type', values = 'name', aggfunc = 'count').reset_index()
PPS = PPS.rename(columns={'name':'counts'})
# bardzo ważne żeby wykres był ładnie posortowany
PPS.sort_values('counts', ascending=False, inplace=True)
PPS.reset_index(inplace=True)
PPS
Out[12]:
In [13]:
# Plot Bars
plt.figure(figsize=(8,4), dpi= 280)
plt.bar(PPS['type'], PPS['counts'], color=['#cfe2f3','#9fc5e8','#6fa8dc'], alpha=0.4, width=.5)
#numery na słupakach
for i, val in enumerate(PPS['counts'].values):
plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':10})
# Decoration
plt.gca().set_xticklabels(PPS['type'], rotation=90, horizontalalignment= 'right')
plt.title("Number of airfields by the type", fontsize=14)
plt.ylabel('counts')
plt.ylim(0, 37000)
plt.show()
In [27]:
import matplotlib.pyplot as plt
color=['#7f6000','#bf9000','#f1c232','#ffd966','#ffe599','#fff2cc']
# Draw plot
fig, ax = plt.subplots(figsize=(7,4), dpi= 280)
ax.vlines(x=PPS.index, ymin=0, ymax=PPS.counts, color=color, alpha=0.7, linewidth=2)
ax.scatter(x=PPS.index, y=PPS.counts, s=75, color='firebrick', alpha=0.7)
# Title, Label, Ticks and Ylim
ax.set_title('Number of airfields by the type', fontdict={'size':10})
ax.set_ylabel('counts')
ax.set_xticks(PPS.index)
ax.set_xticklabels(PPS.type.str.upper(), rotation=90, fontdict={'horizontalalignment': 'right', 'size':8})
#ax.set_ylim(0, 30)
# Annotate
for row in PPS.itertuples():
ax.text(row.Index, row.counts+.5, s=round(row.counts, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=8)
plt.show()
In [28]:
## Draw plot
import matplotlib.patches as patches
color = ['#93c47d','#b6d7a8','#d9ead3','#d0e0e3','#a2c4c9','#76a5af']
fig, ax = plt.subplots(figsize=(16,10), facecolor='white', dpi= 180)
ax.vlines(x=PPS.type, ymin=0, ymax=PPS.counts, color=color, alpha=0.7, linewidth=20)
# Annotate Text
for i, kot in enumerate(PPS.counts):
ax.text(i, kot+0.5, round(kot, 1), horizontalalignment='center', fontsize=18, rotation=90)
# Title, Label, Ticks and Ylim
ax.set_title('Number of airfields by the type', fontdict={'size':22})
ax.set(ylabel='counts', ylim=(0, 40000))
#plt.xticks(PPS.type, PPS.counts.str.upper(), rotation=30, horizontalalignment='right', fontsize=12)
# Add patches to color the X axis labels
#p1 = patches.Rectangle((.57, -0.005), width=.33, height=.13, alpha=.1, facecolor='grey', transform=fig.transFigure)
#p2 = patches.Rectangle((.124, -0.005), width=.446, height=.13, alpha=.1, facecolor='yellow', transform=fig.transFigure)
#fig.add_artist(p1)
#fig.add_artist(p2)
plt.show()
In [16]:
df4 = pd.read_csv('c:/1/WorldHappinessReport.csv')
df4.head(3)# Draw plot
Out[16]:
In [17]:
PKP = df4.pivot_table(index='Region', values='Happiness Score', aggfunc= 'mean' ).reset_index()
PKP['Happiness Score'] = np.round(PKP['Happiness Score'], decimals=2)
# bardzo ważne żeby wykres był ładnie posortowany
PKP.sort_values('Happiness Score', inplace=True)
PKP.reset_index(inplace=True)
PKP
Out[17]:
colors = [‘#d9ead3′,’#b6d7a8′,’#93c47d’,’#6aa84f’]
In [29]:
# Plot Bars
plt.figure(figsize=(8,4), dpi= 280)
plt.bar(PKP['Region'], PKP['Happiness Score'], color=['#d9ead3','#b6d7a8','#93c47d','#6aa84f'], alpha=0.4, width=.9)
#numery na słupakach
for i, val in enumerate(PKP['Happiness Score'].values):
plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':10})
# Decoration
plt.gca().set_xticklabels(PKP['Region'], rotation=90, horizontalalignment= 'right')
plt.title("Average Happiness Rate", fontsize=14)
plt.ylabel('Rate' )
plt.ylim(0, 9)
plt.show()
In [19]:
PKP['HappS'] =PKP['Happiness Score']
In [34]:
import matplotlib.pyplot as plt
# Draw plot
fig, ax = plt.subplots(figsize=(5,3), dpi= 280)
ax.vlines(x=PKP.index, ymin=0, ymax=PKP['HappS'], color='firebrick', alpha=0.7, linewidth=2)
ax.scatter(x=PKP.index, y=PKP['HappS'], s=75, color='firebrick', alpha=0.7)
# Title, Label, Ticks and Ylim
ax.set_title('Average Happiness Rate', fontdict={'size':10})
ax.set_ylabel('Rate')
ax.set_xticks(PKP.index)
#ax.set_xticklabels(PKP.type.str.upper(), rotation=90, fontdict={'horizontalalignment': 'right', 'size':8})
ax.set_ylim(0, 9)
# Annotate
for row in PKP.itertuples():
ax.text(row.Index, row.HappS+.5, s=round(row.HappS, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=8)
plt.gca().set_xticklabels(PKP['Region'], rotation=90, horizontalalignment= 'right')
plt.show()
In [36]:
## Draw plot
import matplotlib.patches as patches
fig, ax = plt.subplots(figsize=(8,5), facecolor='white', dpi= 180)
ax.vlines(x=PKP.index, ymin=0, ymax=PKP['Happiness Score'], color='firebrick', alpha=0.7, linewidth=20)
# Annotate Text
for i, kot in enumerate(PKP['Happiness Score']):
ax.text(i, kot+0.5, round(kot, 1), horizontalalignment='center', fontsize=18, rotation=0)
# Title, Label, Ticks and Ylim
ax.set_title('Average Happiness Rate', fontdict={'size':22})
ax.set(ylabel='Rate', ylim=(0, 9))
plt.xticks(PKP.index, PKP.Region.str.upper(), rotation=30, horizontalalignment='right', fontsize=12)
# Add patches to color the X axis labels
p1 = patches.Rectangle((.57, -0.005), width=.33, height=.13, alpha=.1, facecolor='green', transform=fig.transFigure)
p2 = patches.Rectangle((.124, -0.005), width=.446, height=.13, alpha=.1, facecolor='red', transform=fig.transFigure)
fig.add_artist(p1)
fig.add_artist(p2)
plt.show()