Rozdział 5.1

import warnings
warnings.filterwarnings("ignore")

import pandas.rpy.common as com
import matplotlib.pyplot as plt

kidney = com.load_data('kidney', package='PBImisc')

plt.figure(figsize=(14,6))
plt.plot(kidney['MDRD7'],kidney['MDRD12'],'.')
plt.tight_layout()
plt.savefig('R5_01a.png')

import warnings
warnings.filterwarnings("ignore")

import pandas.rpy.common as com
import matplotlib.pyplot as plt

kidney = com.load_data('kidney', package='PBImisc')

plt.style.use('seaborn')
plt.figure(figsize=(14,6))
plt.plot(kidney['MDRD7'],kidney['MDRD12'],'.')
plt.tight_layout()
plt.savefig('R5_01b.png')

import warnings
warnings.filterwarnings("ignore")

import pandas.rpy.common as com
import matplotlib.pyplot as plt
import seaborn as sns

kidney = com.load_data('kidney', package='PBImisc')

plt.figure(figsize=(14,6))
sns.regplot('MDRD7','MDRD12', data=kidney,  fit_reg=False)
plt.tight_layout()
plt.savefig('R5_01c.png')

Rozdział 5.3.2

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="discrepancy.DR")  
g.map(sns.regplot, 'MDRD7','MDRD12',fit_reg=False)
plt.tight_layout()
plt.savefig('R5_02.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="discrepancy.DR")  
g.map(sns.regplot, 'MDRD7','MDRD12',lowess=True, line_kws={'color': 'red'})
plt.tight_layout()
plt.savefig('R5_03.png')

Rozdział 5.3.4

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import *

kidney = pd.read_csv("kidney.csv")
bins = linspace(0, 150, 10)

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="therapy")
g.map(sns.distplot, 'MDRD12', bins=bins, kde=False)
plt.tight_layout()
plt.savefig('R5_04.png')

Rozdział 5.3.5

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import *

kidney = pd.read_csv("kidney.csv")
u = kidney['therapy'].unique()

plt.figure(figsize=(10,4))
for i in u: ax = sns.kdeplot(kidney[(kidney["therapy"] == i)]['MDRD12'], legend=False)
plt.tight_layout()
plt.savefig('R5_05.png')

Rozdział 5.3.6

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import *

kidney = pd.read_csv("kidney.csv")
u = kidney['therapy'].unique()

plt.figure(figsize=(10,4))
for i in u: ax = sns.kdeplot(kidney[(kidney["therapy"] == i)]['MDRD12'], label=i)
sns.rugplot(kidney['MDRD12'], color='black')
plt.tight_layout()
plt.savefig('R5_06.png')

Rozdział 5.3.7.1

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")[['MDRD7','MDRD12','MDRD36','discrepancy.DR']]
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))
kidney["TF"] = kidney['discrepancy_DR']==0
data_long = pd.melt(kidney.drop('discrepancy_DR', 1), id_vars=['TF','MDRD7'],var_name=['MDRD_12_36'])
print(data_long.head(3))

plt.figure(figsize=(16,4))
g = sns.lmplot(x="MDRD7", y="value", hue="MDRD_12_36", col="TF", data=data_long, lowess=True,ci=None)
plt.tight_layout()
plt.savefig('R5_07.png')
##       TF  MDRD7 MDRD_12_36  value
## 0  False   46.0     MDRD12   65.0
## 1  False   44.0     MDRD12   62.0
## 2  False    6.0     MDRD12   45.0

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
plt.figure(figsize=(16,16))
g = sns.pairplot(kidney, vars=['MDRD7','MDRD3','MDRD12','MDRD36'],markers='.',kind="reg")
plt.tight_layout()
plt.savefig('R5_08.png')

Rozdział 5.3.7.2

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))
kidney['discrepancy_AB_cat'] = pd.Categorical(kidney.discrepancy_AB).labels
plt.figure(figsize=(10,4))
sns.stripplot(y="discrepancy_AB_cat", x="MDRD7", data=kidney, orient='h',jitter=True,order=[4,3,2,1,0])
plt.tight_layout()
plt.savefig('R5_09.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="discrepancy_DR")
g.map(sns.boxplot, "MDRD12", "therapy", sym='k.',flierprops=dict(marker='.', markersize=10))
plt.tight_layout()
plt.savefig('R5_10.png')

Rozdział 5.3.7.3

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))
wPlec = pd.crosstab(index=daneSoc["wyksztalcenie"],columns=daneSoc['plec'])
print(wPlec)

plt.figure(figsize=(16,4))
g =sns.factorplot(y='wyksztalcenie', col='plec', kind='count', data=daneSoc, col_wrap=2, size=3, aspect=1.3, palette='muted')
plt.tight_layout()
plt.savefig('R5_11.png')
## plec           kobieta  mezczyzna
## wyksztalcenie                    
## podstawowe          22         71
## srednie             16         39
## wyzsze              10         24
## zawodowe             7         15

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(10,4))
g =sns.factorplot(y='wyksztalcenie', kind='count', data=daneSoc, size=3, aspect=2.5,  palette='muted',hue="plec")
plt.tight_layout()
plt.savefig('R5_12.png')

Rozdział 5.3.7.4

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.tools.plotting import parallel_coordinates

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))
kidney['group'] = kidney['MDRD7']<30
df = kidney[['MDRD7','MDRD30','MDRD3','MDRD6','MDRD12','MDRD24','MDRD36','MDRD60','group']]

plt.figure(figsize=(10,6))
parallel_coordinates(df, 'group')
plt.tight_layout()
plt.savefig('R5_14.png')

Rozdział 5.3.7.5

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import *

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

bins = linspace(0, 100, 10)
plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="therapy")
g.map(sns.distplot, 'MDRD7', bins=bins, kde=False)
plt.tight_layout()
plt.savefig('R5_15.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="diabetes", hue="therapy", aspect=1.3)
g.map(sns.kdeplot, 'MDRD7', cumulative=True)
plt.legend()
plt.tight_layout()
plt.savefig('R5_16.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(16,4))
g = sns.FacetGrid(kidney, col="diabetes", hue="therapy", aspect=1.3)
g.map(sns.distplot, 'MDRD7', hist=False, rug=True)
plt.legend()
plt.tight_layout()
plt.savefig('R5_17.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import probscale

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(16,4))
fg = (
    sns.FacetGrid(data=kidney, col="diabetes", hue="therapy", margin_titles=True, aspect=1.3)
        .map(probscale.probplot, 'MDRD7', probax='x', plottype='qq', bestfit=False)
        .set_ylabels('Probability')
        .add_legend()
)
plt.tight_layout()
plt.savefig('R5_18.png')

Rozdział 5.3.7.6

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))
c0 = (kidney["diabetes"] == 0)
c1 = (kidney["diabetes"] == 1)
df0 = kidney[c0]
df1 = kidney[c1]

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')
ax1.scatter(df0['MDRD30'],df0['MDRD12'],df0['MDRD7'], marker='+')
ax2.scatter(df1['MDRD30'],df1['MDRD12'],df1['MDRD7'], marker='+')
ax1.set_title('diabetes 0'); ax2.set_title('diabetes 1')
ax1.set_xlabel('MDRD30'); ax2.set_xlabel('MDRD30')
ax1.set_ylabel('MDRD12'); ax2.set_ylabel('MDRD12')
ax1.set_zlabel('MDRD7'); ax2.set_zlabel('MDRD7')
plt.tight_layout()
plt.savefig('R5_19.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
sns.kdeplot(kidney['MDRD7'],kidney['MDRD30'], n_levels=20, cmap="Purples_d", shade=True,ax=ax1)
sns.kdeplot(kidney['MDRD7'],kidney['MDRD30'], n_levels=20, cmap="Purples_d",ax=ax2)
plt.tight_layout()
plt.savefig('R5_20.png')

import pandas as pd
from scipy import *
import scipy.stats as stats
import matplotlib.pyplot as plt

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

data = vstack([kidney['MDRD7'],kidney['MDRD30']])
kde = stats.kde.gaussian_kde(data)
xgrid = linspace(kidney['MDRD7'].min(),kidney['MDRD7'].max(),100)
ygrid = linspace(kidney['MDRD30'].min(),kidney['MDRD30'].max(),100)
Xgrid, Ygrid = meshgrid(xgrid,ygrid)
Z = kde.evaluate(vstack([Xgrid.ravel(),Ygrid.ravel()]))

fig = plt.figure(figsize=(14,6))
plt.subplot(121)
plt.imshow(Z.reshape(Xgrid.shape),origin='lower',extent=[kidney['MDRD7'].min(),kidney['MDRD7'].max(),kidney['MDRD30'].min(),kidney['MDRD30'].max()],cmap='RdGy')
cb = plt.colorbar()
plt.xlabel("MDRD7"); plt.ylabel("MDRD30")
plt.subplot(122)
contours = plt.contour(Z.reshape(Xgrid.shape))
plt.clabel(contours,inline=True,fontsize=8,fmt='%.5f')
plt.xlabel("MDRD7"); plt.ylabel("MDRD30")
plt.tight_layout()
plt.savefig('R5_21.png')

import pandas as pd
from scipy import *
import scipy.stats as stats
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

data = vstack([kidney['MDRD7'],kidney['MDRD30']])
kde = stats.kde.gaussian_kde(data)
xgrid = linspace(kidney['MDRD7'].min(),kidney['MDRD7'].max(),100)
ygrid = linspace(kidney['MDRD30'].min(),kidney['MDRD30'].max(),100)
Xgrid, Ygrid = meshgrid(xgrid,ygrid)
Z = kde.evaluate(vstack([Xgrid.ravel(),Ygrid.ravel()]))

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')
surf = ax1.plot_surface(Xgrid, Ygrid,Z.reshape(Xgrid.shape), rstride=1, cstride=1,
                      cmap=cm.RdBu,linewidth=0, antialiased=False)
ax1.view_init(30, 230)
ax1.set_xlabel('MDRD7');ax1.set_ylabel('MDRD30')
surf = ax2.plot_surface(Xgrid, Ygrid,Z.reshape(Xgrid.shape), rstride=2, cstride=2, color="white", shade=False, edgecolor="blue")
ax2.view_init(30, 230)
ax2.set_xlabel('MDRD7');ax2.set_ylabel('MDRD30')
plt.tight_layout()
plt.savefig('R5_22.png')

Rozdział 5.3.8.1

import warnings
warnings.filterwarnings("ignore")

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")
sns.lmplot('sepal_length', 'petal_length', iris, fit_reg=False,  col='species', sharex=False, sharey=False)
plt.tight_layout()
plt.savefig('R5_23.png')

Rozdział 5.3.8.2

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(16,4))
g =sns.factorplot(y='wyksztalcenie', kind='count', data=daneSoc, size=3, aspect=1.3,  palette='muted',col="plec",hue='praca')
plt.tight_layout()
plt.savefig('R5_13.png')

Rozdział 5.3.10.1

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("ticks")

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

fig = plt.figure(figsize=(16,10))
grid = plt.GridSpec(4,4)
ax1= plt.subplot(grid[:4, :2])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax1)
ax2= plt.subplot(grid[:2, 2:])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax2)
ax3= plt.subplot(grid[2:, 2])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax3)
ax4= plt.subplot(grid[2:, 3:])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax4)
plt.savefig('R5_24.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("ticks")

kidney = pd.read_csv("kidney.csv")
kidney = kidney.rename(columns=lambda x: x.replace('.', '_'))

fig = plt.figure(figsize=(16,10))
ax1 = fig.add_axes([0.05,0.05, 0.6, 0.6])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax1)
ax2 = fig.add_axes([0.4,0.4, 0.45, 0.45])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax2)
ax3 = fig.add_axes([0.7, 0.7, 0.25, 0.25])
sns.regplot('MDRD12', 'MDRD7', kidney, fit_reg=False, ax=ax3)
plt.savefig('R5_25.png')

Rozdział 5.3.10.2*

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
sns.set_style("ticks")

data_loader = sm.datasets.sunspots.load_pandas()
df = data_loader.data

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(df['SUNACTIVITY'].iloc[140:279,])
ax2.plot(df['SUNACTIVITY'].iloc[0:139,])
plt.savefig('R5_26.png')

Rozdział 5.4.2

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

print(countries.head())

fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
sns.regplot('birth_rate', 'death_rate', data=countries, fit_reg=False, scatter_kws={'color': 'navy'}, marker='+',ax=ax1)
sns.regplot('birth_rate', 'death_rate', data=countries, fit_reg=True, scatter_kws={'color': 'navy'},line_kws={'color': 'red'},ci=None,ax=ax2)
sns.regplot('birth_rate', 'death_rate', data=countries, lowess=True, scatter_kws={'color': 'navy'},line_kws={'color': 'red'},ax=ax3)
plt.tight_layout()
plt.savefig('R5_27.png')
##        country  birth_rate  death_rate  population continent
## 0  Afghanistan        34.1         7.7       30552      Asia
## 1      Albania        12.9         9.4        3173    Europe
## 2      Algeria        24.3         5.7       39208    Africa
## 3      Andorra         8.9         8.4          79    Europe
## 4       Angola        44.1        13.9       21472    Africa

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(10,6))
sns.violinplot(x="continent", y="birth_rate", data=countries, scale='width',inner='box')
sns.stripplot(x="continent", y="birth_rate", data=countries, jitter=True, color='black',alpha=0.2)
plt.tight_layout()
plt.savefig('R5_28.png')

Rozdział 5.4.3

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

labels = countries['continent'].unique()
tab = [countries[(countries["continent"] == labels[i])] for i in range(5)]

plt.figure(figsize=(10,6))
for i in range(5): plt.scatter(tab[i]["birth_rate"], tab[i]["death_rate"], s=tab[i]["population"]**0.5, alpha=0.5,label=labels[i])
plt.legend()
plt.xlabel('birth_rate')
plt.ylabel('death_rate')
plt.tight_layout()
plt.savefig('R5_29.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(10,6))
points = plt.scatter(countries["birth_rate"], countries["death_rate"],
                     c=countries["birth_rate"], s=75, cmap="BuGn")
plt.colorbar(points)
sns.regplot("birth_rate", "death_rate", data=countries, scatter=False, color=".1", fit_reg=False)
plt.tight_layout()
plt.savefig('R5_30.png')

Rozdział 5.4.4

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))
nam = ['Africa','Americas','Asia','Europe','Oceania']

fig = plt.figure(figsize=(10,8))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
sns.boxplot(y="birth_rate", x="continent", data=countries,color='lightyellow',linewidth=0.5, order=nam, sym='k.',flierprops=dict(marker='.', markersize=10), ax=ax1)
sns.swarmplot(y="birth_rate", x="continent", order=nam, data=countries, ax=ax2)
sns.violinplot(y="birth_rate", x="continent", data=countries,color='lightyellow',linewidth=0.5, order=nam, scale='width',inner=None, ax=ax3)
sns.regplot(x="birth_rate", y="death_rate", data=countries, fit_reg=False, ax=ax4)
plt.tight_layout()
plt.savefig('R5_31.png')

Rozdział 5.4.5

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))
nam = ['Africa','Americas','Asia','Europe','Oceania']

fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(121)
(countries.groupby('continent').size().sort_values().plot(kind='barh'))
ax2 = fig.add_subplot(122)
sns.regplot(x="birth_rate", y="death_rate", data=countries, lowess=True, line_kws={'color': 'red'}, ax=ax2)
sns.regplot(x="birth_rate", y="death_rate", data=countries,  scatter=False, ax=ax2)
plt.tight_layout()
plt.savefig('R5_32.png')

Rozdział 5.4.6*

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))
nam = ['Africa','Americas','Asia','Europe','Oceania']

g = sns.FacetGrid(countries,col='continent',col_order=nam, size=5, aspect=0.5)
g.map(plt.scatter, 'birth_rate', 'death_rate')
plt.tight_layout()
plt.savefig('R5_33.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))
nam = ['Africa','Americas','Asia','Europe','Oceania']

g = sns.FacetGrid(countries,col='continent',col_order=nam, size=5, aspect=0.5)
g.map(plt.scatter, 'birth_rate', 'death_rate')
for ax in g.axes.flat:
    ax.plot(countries['birth_rate'], countries['death_rate'], '.', color='red', alpha=0.3)
plt.tight_layout()
plt.savefig('R5_34.png')

Rozdział 5.4.7*

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(10,6))
sns.lmplot(x="birth_rate", y="death_rate", hue="continent", data=countries, markers=['s','+','o','^','*'], ci=None, fit_reg=False, size=3, aspect=2.5)
plt.tight_layout()
plt.savefig('R5_35.png')

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121)
sns.regplot(x="birth_rate", y="death_rate", data=countries, fit_reg=False, ax=ax1)
plt.xlim(0,50)
plt.xticks([1,2,5,10,20,50])
plt.tight_layout()
ax2 = fig.add_subplot(122)
sns.regplot(x="birth_rate", y="death_rate", data=countries, fit_reg=False, ax=ax2)
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()
plt.tight_layout()
plt.savefig('R5_36.png')

Rozdział 5.4.8

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121)
sns.regplot(x="birth_rate", y="death_rate", data=countries, fit_reg=False,ax=ax1)
plt.xscale('log', basex=2)
plt.yscale('log', basex=2)
ax2 = fig.add_subplot(122)
sns.regplot(x="birth_rate", y="death_rate", data=countries, fit_reg=False, ax=ax2)
plt.axis('equal')
plt.tight_layout()
plt.savefig('R5_37.png')

import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

lowess = sm.nonparametric.lowess
z = lowess(countries["death_rate"],countries["birth_rate"])

fig = plt.figure(figsize=(16,8))
plt.style.use('ggplot')
ax1 = fig.add_subplot(121)
ax1.plot(countries["birth_rate"], countries["death_rate"],'o')
ax1.plot(z[:,0],z[:,1],'orange',lw=4)
plt.title('ggplot style')
plt.xlabel('birth_rate')
plt.ylabel('death_rate')
plt.style.use('default')
ax2 = fig.add_subplot(122)
ax2.plot(countries["birth_rate"], countries["death_rate"],'o')
ax2.plot(z[:,0],z[:,1],'orange',lw=4)
plt.title('default style')
plt.xlabel('birth_rate')
plt.ylabel('death_rate')
plt.tight_layout()
plt.savefig('R5_38.png')

import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm

countries = pd.read_csv("countries.csv")
countries = countries.rename(columns=lambda x: x.replace('.', '_'))

lowess = sm.nonparametric.lowess
z = lowess(countries["death_rate"],countries["birth_rate"])

fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111)
ax1.plot(countries["birth_rate"], countries["death_rate"],'o')
ax1.plot(z[:,0],z[:,1],'orange',lw=4)
ax2 = fig.add_axes([0.55,0.55, 0.3, 0.3])
ax2.plot(countries["birth_rate"], countries["death_rate"],'o')
ax2.plot(z[:,0],z[:,1],'orange',lw=4)
ax3 = fig.add_axes([0.7, 0.7, 0.1, 0.1])
ax3.plot(countries["birth_rate"], countries["death_rate"],'o')
ax3.plot(z[:,0],z[:,1],'orange',lw=4)
plt.tight_layout()
plt.savefig('R5_39.png')
## /home/krz/.local/lib/python3.5/site-packages/matplotlib/figure.py:1742: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
##   warnings.warn("This figure includes Axes that are not "

Rozkład 5.5.1

import matplotlib.pyplot as plt
from scipy import *

x = arange(-2*pi, 2*pi, 0.3)

fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111)
ax1.plot(x, sin(x), 'C3-o')
ax1.plot(x, cos(x), color='C0')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Wykres funkcji sin(x) i cos(x)')
plt.tight_layout()
plt.savefig('R5_40.png')

import matplotlib.pyplot as plt
from scipy import *

x = arange(-2.25, 2.25, 0.3)

fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(111)
for i in range(1,10+1,1): ax.plot(x, i*x)
ax.axhline(y=-1, linewidth=3, color='C3')
ax.axhline(y=0, color='black')
ax.axvline(x=-1, linewidth=2, color='C0', linestyle='--')
ax.text(1.7, 0.2, 'a=0, b=0', verticalalignment='center', horizontalalignment='left')
ax.text(1.4, 1.1, 'a=1, b=0', verticalalignment='center', horizontalalignment='left')
ax.text(1, 1.7, 'a=2, b=0', verticalalignment='center', horizontalalignment='left')
ax.text(1.7, -0.8, 'h = -1', verticalalignment='center', horizontalalignment='left')
ax.text(-0.9, 1.1, 'v = -1', verticalalignment='center', horizontalalignment='left')
plt.xlim(-2.25,2.25)
plt.ylim(-2.25,2.25)
plt.title('Wariacje z funkcjÄ… plot()')
plt.tight_layout()
plt.savefig('R5_41.png')

Rozdział 5.5.3

import matplotlib.pyplot as plt
from scipy import *

ruchBrowna = random.normal(0, 1, size=(200,4))
ruchBrowna = apply_along_axis(cumsum, 0, ruchBrowna)

plt.figure(figsize=(10,6))
plt.plot(ruchBrowna)
plt.title('Ruch Browna')
plt.tight_layout()
plt.savefig('R5_42.png')

Rozdział 5.5.4

import matplotlib.pyplot as plt
from scipy import *
import matplotlib.patches as mpatches

fig = plt.figure(figsize=(6,6))
ax1 = fig.add_axes([0.1,0.3,0.8,0.6])
x = arange(0, 5, 0.1)
y1 = 0.3*x+4.5
y2 = 0.3*x**2+4.5
y3 = 0.3*x**3+4.5
y4 = 0.3*x**4+4.5
ax1.plot(x,y1);ax1.plot(x,y2);ax1.plot(x,y3);ax1.plot(x,y4)
ax1.set_xlabel('independent values')
ax1.set_ylabel('dependent values')
ax1.set_xlim(0,6)
ax1.set_ylim(4.5,8.5)
plt.xticks([1,2,3,4,5],['przed','chwile przed','w trakcie','chwile po','po'], rotation='vertical')
plt.text(0, 8.75, '$\exp(\cos(x^2)-x)$', verticalalignment='center', horizontalalignment='left')
plt.text(4.5, 3.5, '$\cos(x)^2+\sin(x)^2$', verticalalignment='center', horizontalalignment='left',fontsize=15)
ax1.set_yticks([5,6,7,8])
ax2 = ax1.twinx()
ax2.set_ylim([4.5,8.5])
ax2.set_yticks([4.5,5,5.5,6,6.5,7,7.5,8,8.5])
ax2.set_ylabel('2nd value axis')
patch1 = mpatches.Patch(color='C0', label='$x$')
patch2 = mpatches.Patch(color='C1', label='$x^2$')
patch3 = mpatches.Patch(color='C2', label='$x^3$')
patch4 = mpatches.Patch(color='C3', label='$x^4$')
plt.legend(handles=[patch1,patch2,patch3,patch4],ncol=2)
plt.savefig('R5_43.png')

Rozdział 5.5.6

import matplotlib.pyplot as plt
from scipy import *

plt.figure(figsize=(10,6))

def plusk(x,y,s1,s2):
    return sin(sqrt((x-s1)**2+(y-s2)**2)/4)/(abs(x-s1)+abs(y-s2)+25)

xx, yy = meshgrid(linspace(1,200,200), linspace(200,1,200))

mat1 = plusk(xx,yy,100,50).real
mat2 = plusk(xx,yy,50,100).real
mat3 = plusk(xx,yy,20,20).real

mat = mat1 + mat2 + mat3

i = plt.imshow(mat, interpolation='catrom')
plt.xticks(())
plt.yticks(())
plt.tight_layout()
plt.savefig('R5_44.png')

Rozdział 5.5.8.3

import matplotlib.pyplot as plt
from colour import *

c = Color("orange")
print('RGB: ',c.rgb)
print('hex: ',c.hex)
print('hsl: ',c.hsl)
## RGB:  (1.0, 0.6470588235294115, 0.0)
## hex:  #ffa500
## hsl:  (0.10784313725490191, 1.0, 0.5)

Rozdział 5.5.9

import matplotlib.pyplot as plt
from scipy import *

x = linspace(0,4,100)

fig = plt.figure(figsize=(14,6))
plt.subplot(121)
plt.plot(-x,x,ls='--',label='--')
plt.plot(-x,x+1,ls='-.',label='-.')
plt.plot(-x,x+2,ls=':',label=':')
plt.title('linestyle =')
plt.legend()
plt.subplot(122)
plt.plot(x,x,ls='--', dashes=(5,2),label='(5,2)')
plt.plot(x,x+1,ls='--', dashes=(5,2,20,2),label='(5,2,20,2)')
plt.plot(x,x+2,ls='--', dashes=(2,5),label='(2,5)')
plt.title("linestyle = '--' , dashes = ")
plt.legend()
plt.tight_layout()
plt.savefig('R5_45.png')

import matplotlib.pyplot as plt
from scipy import *

x = linspace(0,20,20)

plt.figure(figsize=(10,6))
plt.plot(x,x+12, 'o-',label='plot')
markerline, stemlines, baseline = plt.stem(x, x+10, '-',markerfmt=' ', linefmt='C1',basefmt='none',label='stem')
plt.setp(stemlines, 'linewidth', 5)
plt.step(x,x+14, label='step', linewidth= 5)
plt.legend()
plt.tight_layout()
plt.savefig('R5_46.png')

Rozdział 5.5.10

import matplotlib.pyplot as plt

plt.figure(figsize=(10,6))
plt.plot([1],[4],"r.",markersize=20)
plt.plot([2],[4],"o",markeredgecolor='red',markerfacecolor='none',markersize=20)
plt.plot([3],[4],"s",markeredgecolor='red',markerfacecolor='none',markersize=20)
plt.plot([4],[4],"D",markeredgecolor='red',markerfacecolor='none',markersize=20)
plt.plot([5],[4],"^",markeredgecolor='red',markerfacecolor='none',markersize=20)
plt.plot([1],[3],"s",markersize=20);plt.plot([2],[3],"o",markersize=20);plt.plot([3],[3],"^",markersize=20);plt.plot([4],[3],"D",markersize=20);plt.plot([5],[3],"o",markersize=20)
plt.plot([1],[2],">",markersize=20);plt.plot([2],[2],"p",markersize=20);plt.plot([3],[2],"*",markersize=20);plt.plot([4],[2],"+",markersize=20);plt.plot([5],[2],"x",markersize=20)
plt.scatter([3], [1], marker="s", s=200, facecolors='none', edgecolors='b')
plt.title('marker=')
plt.tight_layout()
plt.savefig('R5_47.png')

Rozdział 5.5.11

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig = plt.figure(figsize=(14,6))
ax1 = fig.add_subplot(121)
ax1.fill_between([0,1,2,3,4,5,6,6],[1,2,1,2,1,2,1,0.75],edgecolor="k",linewidth=2)
ax1.fill_between([0,6],[0.25,1],hatch="X",edgecolor="red", facecolor='C0', linewidth=2)
plt.title("fill_between")

ax2 = fig.add_subplot(122, aspect='equal')
ax2.plot([-1,6],[-1,6])
ax2.text(2,3, 'dowolny tekst', fontsize=16,
               rotation=45, rotation_mode='anchor')
ax2.annotate('Vmax', xy=(0,3),xytext=(3,5.75),\
arrowprops=dict(arrowstyle='->',color='#d62728'),color='C0')
ax2.add_patch(
    patches.Ellipse((4, 1), 3, 1.5, edgecolor='C1', fc='None',hatch='/',
    lw=2, ls='--',angle=45))
ax2.fill([-1,-1,0],[-1,2,3], color="C2",alpha=0.2)
plt.title('text(), annotate(), arrowprops(), Ellipse(), fill()')
plt.tight_layout()
plt.savefig('R5_48.png')

Rozdział 5.5.13.2

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

plt.figure(figsize=(14, 6))
gs = gridspec.GridSpec(2, 5)
ax1 = plt.subplot(gs[0, 0:2])
ax2 = plt.subplot(gs[0, 2:4])
ax3 = plt.subplot(gs[:, 4])
ax4 = plt.subplot(gs[1, 0:2])
ax5 = plt.subplot(gs[1, 2:4])
ax1.text(0.5, 0.5, 'Axes 1', ha='center', va='center', size=24, alpha=.5)
ax2.text(0.5, 0.5, 'Axes 2', ha='center', va='center', size=24, alpha=.5)
ax3.text(0.5, 0.5, 'Axes 3', ha='center', va='center', size=24, alpha=.5)
ax4.text(0.5, 0.5, 'Axes 4', ha='center', va='center', size=24, alpha=.5)
ax5.text(0.5, 0.5, 'Axes 5', ha='center', va='center', size=24, alpha=.5)
plt.tight_layout()
plt.savefig('R5_49.png')

Rozdział 5.5.13

import pandas as pd
import matplotlib.pyplot as plt

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(6,6))
plt.hist2d(daneSoc['cisnienie_skurczowe'],daneSoc['cisnienie_rozkurczowe'], bins=(10, 10))
plt.tight_layout()
plt.savefig('R5_50.png')

Rozdział 5.5.15

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(6,6))
g = sns.pairplot(daneSoc[['wiek','cisnienie_skurczowe','cisnienie_rozkurczowe']], diag_kind="kde",markers=".",kind="reg")
plt.tight_layout()
plt.savefig('R5_51.png')

Rozdział 5.5.16

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
from scipy import *
import matplotlib.pyplot as plt
from biokit import *

df = pd.read_csv("Iris.csv")

fig = plt.figure(figsize=(16,8))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
c = corrplot.Corrplot(df)
c.plot(fig=fig, ax=ax1, colorbar=False)
c.plot(fig=fig, ax=ax2, method='text', fontsize=9, colorbar=False)
plt.savefig('R5_52.png')
## Computing correlation

Rozdział 5.5.17

import warnings
warnings.filterwarnings("ignore")

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

daneSoc = pd.read_csv("daneSoc.csv")
daneSoc = daneSoc.rename(columns=lambda x: x.replace('.', '_'))

plt.figure(figsize=(6,6))
g = sns.jointplot(x="cisnienie_skurczowe", y="cisnienie_rozkurczowe", data=daneSoc, kind="kde", color="orange")
g.plot_joint(plt.scatter, c="C0", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("$X$", "$Y$")
plt.savefig('R5_53.png')

Rozdział 5.5.21

import warnings
warnings.filterwarnings("ignore")

from scipy import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("APPL.csv")

df = df.join(df['Date'].str.split('-', 2, expand=True).\
rename(columns={0:'Rok', 1:'Miesiac', 2:'Dzien'}))
df['Date'] = pd.to_datetime(df['Date'])
t = df.groupby(['Rok','Miesiac'],as_index=False)['Close'].agg({'med' : 'median'})
d = t.pivot("Miesiac", "Rok", "med")

fig = plt.figure(figsize=(10,8))
g = sns.heatmap(d)
plt.savefig('R5_54.png')

import warnings
warnings.filterwarnings("ignore")

from scipy import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("Iris.csv")
corr = df.corr()

fig = plt.figure(figsize=(10,8))
cmap = sns.blend_palette(["#00008B", "#6A5ACD", "#F0F8FF",    "#FFE6F8", "#C71585", "#8B0000"], as_cmap=True)
sns.corrplot(corr, annot=False, sig_stars=False,  diag_names=False, cmap=cmap)
plt.tight_layout()
plt.savefig('R5_55.png')

import warnings
warnings.filterwarnings("ignore")

from scipy import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from biokit.viz import Heatmap

df = pd.read_csv("APPL.csv")

df = df.join(df['Date'].str.split('-', 2, expand=True).\
rename(columns={0:'Rok', 1:'Miesiac', 2:'Dzien'}))
df['Date'] = pd.to_datetime(df['Date'])
t = df.groupby(['Rok','Miesiac'],as_index=False)['Close'].agg({'med' : 'median'})
d = t.pivot("Miesiac", "Rok", "med")

fig = plt.figure(figsize=(16,8))
h = Heatmap(d)
layout = h.plot()
plt.savefig('R5_56.png')

import warnings
warnings.filterwarnings("ignore")

from scipy import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from biokit.viz import Heatmap

df = pd.read_csv("Iris.csv")
corr = df.corr()

fig = plt.figure(figsize=(16,8))
h = Heatmap(corr)
layout = h.plot()
plt.savefig('R5_57.png')