Skip to content
Snippets Groups Projects
Commit 97438e63 authored by Christof Kaufmann's avatar Christof Kaufmann
Browse files

Notebooks from applied-cs/data-science@caa29152

parent 708d13b0
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Code zu Folien
Dieses Skript bzw. Jupyter-Notebook enthält den Code, der auch auf den Folien "Pandas & Seaborn" enthalten ist. Zum Vorbereiten, Mitmachen oder Nacharbeiten.
%% Cell type:code id: tags:
```
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
```
%% Cell type:code id: tags:
```
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
df = pd.read_csv(url)
# offline-Alternative:
# from sklearn.datasets import load_iris
# df = pd.concat(load_iris(return_X_y=True, as_frame=True), axis='columns')
# df.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
# df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
df['species'] = df['species'].astype('category')
df
```
%% Cell type:code id: tags:
```
print(df.shape)
print(df.columns)
print(df.dtypes)
print(df.index)
df.info()
```
%% Cell type:code id: tags:
```
display(df.describe())
display(df.describe(exclude='number'))
```
%% Cell type:code id: tags:
```
counts = df['species'].value_counts()
display(counts)
counts.plot.pie(startangle=60, autopct='{:.2f}%'.format)
plt.ylabel('species')
```
%% Cell type:code id: tags:
```
col = ['petal_length']
df.plot.hist(column=col)
df.plot.hist(column=col, range=(1, 7), bins=12)
df.plot.hist(column=col, range=(1, 7), bins=12, by='species', sharex=True)
```
%% Cell type:code id: tags:
```
df.hist(layout=(1, 4))
```
%% Cell type:code id: tags:
```
X = df.drop(columns='species')
y = df['species']
fig, axs = plt.subplots(1, 4)
for c in y.unique():
axs = X[y == c].hist(ax=axs, alpha=0.5)
```
%% Cell type:code id: tags:
```
plt.figure()
sns.kdeplot(data=df, x='petal_length', fill=True)
plt.figure()
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True, alpha=0.5)
fig, axs = plt.subplots(1, 4, figsize=(10, 3), constrained_layout=True)
for ax, col in zip(axs, df.columns[:-1]):
sns.kdeplot(data=df, x=col, hue='species', fill=True, alpha=0.5, ax=ax)
if col != 'petal_width':
ax.get_legend().set_visible(False)
```
%% Cell type:code id: tags:
```
df.boxplot(column='petal_length', by='species')
```
%% Cell type:code id: tags:
```
fig, axs = plt.subplots(2, 2, sharey=False) # y-Achsen unabhängig
pd.plotting.boxplot(df, by='species', ax=axs) # übergebe axs
[ax.set_xlabel('') for ax in axs.ravel()] # entferne x-Labels
fig.tight_layout()
```
%% Cell type:code id: tags:
```
import seaborn as sns
sns.violinplot(hue='species', y='petal_length', data=df)
```
%% Cell type:code id: tags:
```
df.plot.scatter(x='petal_length', y='petal_width', c='species', colormap='viridis', alpha=0.7)
```
%% Cell type:code id: tags:
```
sns.pairplot(df, hue='species', plot_kws={'alpha': 0.5})
```
%% Cell type:code id: tags:
```
pd.plotting.parallel_coordinates(df, 'species', colormap='viridis', alpha=.5)
```
%% Cell type:code id: tags:
```
from sklearn.preprocessing import minmax_scale
num_cols = df.columns.drop('species')
df_scaled = df.copy()
df_scaled[num_cols] = minmax_scale(df[num_cols])
pd.plotting.parallel_coordinates(df_scaled, 'species', colormap='viridis', alpha=.5)
```
%% Cell type:code id: tags:
```
import numpy as np
from matplotlib.path import Path
import matplotlib.patches as patches
ys = df.drop(columns='species')
ynames = ys.columns
ys = ys.to_numpy()
ymins = ys.min(axis=0)
ymaxs = ys.max(axis=0)
dys = ymaxs - ymins
ymins -= dys * 0.05 # add 5% padding below and above
ymaxs += dys * 0.05
# reverse axis 1 to have less crossings
# ymaxs[1], ymins[1] = ymins[1], ymaxs[1]
# dys = ymaxs - ymins
# transform all data to be compatible with the main axis
zs = np.zeros_like(ys)
zs[:, 0] = ys[:, 0]
zs[:, 1:] = (ys[:, 1:] - ymins[1:]) / dys[1:] * dys[0] + ymins[0]
fig, host = plt.subplots(figsize=(10, 4))
axes = [host] + [host.twinx() for i in range(ys.shape[1] - 1)]
for i, ax in enumerate(axes):
ax.set_ylim(ymins[i], ymaxs[i])
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
if ax != host:
ax.spines['left'].set_visible(False)
ax.yaxis.set_ticks_position('right')
ax.spines["right"].set_position(("axes", i / (ys.shape[1] - 1)))
host.set_xlim(0, ys.shape[1] - 1)
host.set_xticks(range(ys.shape[1]))
host.set_xticklabels(ynames, fontsize=14)
host.tick_params(axis='x', which='major', pad=7)
host.spines['right'].set_visible(False)
host.xaxis.tick_top()
# host.set_title('Parallel Coordinates Plot — Iris', fontsize=18, pad=12)
colors = plt.cm.viridis([0, 128, 255])
target_names = df['species'].unique()
target = df['species'].cat.codes
legend_handles = [None for _ in target_names]
for j in range(ys.shape[0]):
# create bezier curves
verts = list(zip([x for x in np.linspace(0, len(ys) - 1, len(ys) * 3 - 2, endpoint=True)],
np.repeat(zs[j, :], 3)[1:-1]))
codes = [Path.MOVETO] + [Path.CURVE4 for _ in range(len(verts) - 1)]
path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='none',
lw=2, alpha=0.5, edgecolor=colors[target[j]])
legend_handles[target[j]] = patch
host.add_patch(patch)
host.legend(legend_handles, target_names,
loc='lower center', bbox_to_anchor=(0.5, -0.18),
ncol=len(target_names), fancybox=True, shadow=True)
```
%% Cell type:code id: tags:
```
import plotly.express as px
# fig = px.parallel_coordinates(df, color="species", labels={'species': tuple('ABC')})
fig = px.parallel_coordinates(df, color=df["species"].cat.codes)
fig.data[0]['dimensions'][-1]['label'] = 'species'
fig.show()
```
%% Cell type:code id: tags:
```
cp = df.copy()
cp.loc[1, 'sepal_width'] = 1
cp.loc[0:2, 'petal_length'] = 2
cp.loc[0, 'sepal_width':'petal_width'] = 3
cp.loc[1:, 'sepal_length'] = 4
cp.loc[:2, :'sepal_width'] = 5
cp.loc[:49, :].to_csv('iris-setosa.csv')
cp
```
%% Cell type:code id: tags:
```
display(df.loc[[0, 149, 2], 'petal_width'])
part = df.loc[[0, 149, 2], ['petal_width', 'sepal_width']]
display(part)
```
%% Cell type:code id: tags:
```
display(part.iloc[1, -1])
display(part.iloc[:2, -1])
display(part.iloc[[0, 1], [0, 1]])
```
%% Cell type:code id: tags:
```
pw = part.loc[:, 'petal_width'] <= 1
sw = part.loc[:, 'sepal_width'] < 3.5
display(pw)
display(sw)
display(~sw)
display(part.loc[pw & sw])
display(part.loc[pw | ~sw])
display(part.loc[pw ^ sw])
```
%% Cell type:code id: tags:
```
display(part.drop(index=149, columns='petal_width'))
display(part.drop(index=[149, 0]))
```
%% Cell type:code id: tags:
```
part.loc[3] = [2, 6]
display(part)
part.loc[:, 'weight'] = [1, 2, 3, 4]
display(part)
```
%% Cell type:code id: tags:
```
a = part.drop(index=3)
b = df.loc[:2, ['petal_length', 'petal_width']]
display(a)
display(b)
display(pd.concat((a, b), axis='columns'))
display(pd.concat((a, b), axis='index'))
```
%% Cell type:code id: tags:
```
df = pd.DataFrame({'name': ['Paul', 'John', 'Bill'], 'type': ['student', 'student', 'teacher']})
# kein Effekt, denn name wird name zugewiesen und type wird type zugewiesen
df.loc[:, ['name', 'type']] = df.loc[:, ['type', 'name']]
print(df)
print()
# kein Effekt, denn name wird name zugewiesen und type wird type zugewiesen
df.loc[:, ['name', 'type']] = df[['type', 'name']]
print(df)
print()
# tauscht Werte, weil __index__ (also []) kein Alignment hat
df[['name', 'type']] = df[['type', 'name']]
print(df)
print()
# tauscht Werte, weil auf der rechten Seite ein Numpy-Array steht.
df.loc[:, ['name', 'type']] = df[['type', 'name']].to_numpy()
print(df)
print()
# spaltenweise Zuweisung (ohne extra Klammern) ergibt Series, also werden keine Spalten aligned
temp = df['name'].copy() # kopiere die Werte, damit sie im nächsten Schritt nicht überschrieben werden
df.loc[:, 'name'] = df['type'] # tauscht Werte, weil es hier keine Spalten zu alignen gibt (sondern nur Zeilen, die aber zueinander passen) ...
df.loc[:, 'type'] = temp # ... auf der rechten und linken Seite steht jeweils eine pd.Series
print(df)
print()
# wegen Alignment hat Zeile 0 keinen Partner und bekommt NaN. Zeile 1 bekommt Zeile 1 zugewiesen.
df.loc[[0]] = df.loc[[1, 2]]
print(df)
print()
# wegen Alignment hat Zeile 0 keinen Partner und bekommt NaN. Zeile 1 bekommt Zeile 1 zugewiesen.
df.loc[[0, 1]] = df.loc[[1, 2]].to_numpy()
print(df)
print()
# wegen Alignment hat Spalte name keinen Partner und bekommt NaN.
df.loc[:, ['name']] = df[['type']]
print(df)
print()
# hier findet kein Alignment der Spalten statt, weil auf der rechten Seite nur eine pd.Series steht, es also keine Spalten zu alignen gibt
df.loc[:, ['name']] = df['type']
print(df)
print()
```
%% Cell type:code id: tags:
```
df['species']
df['species'].info()
```
%% Cell type:code id: tags:
```
X = df.drop(columns='species')
y = df['species']
display(X.mean())
display(y.value_counts())
```
%% Cell type:code id: tags:
```
species_means = X.groupby(y).mean()
display(species_means)
diff = species_means - [6, 3, 2, 0.5]
(diff**2).sum(axis='columns')
```
......
......@@ -6,6 +6,7 @@
# %% import Pandas
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
......@@ -44,6 +45,40 @@ counts.plot.pie(startangle=60, autopct='{:.2f}%'.format)
plt.ylabel('species')
# %% Histogramm mit einem Feature
col = ['petal_length']
df.plot.hist(column=col)
df.plot.hist(column=col, range=(1, 7), bins=12)
df.plot.hist(column=col, range=(1, 7), bins=12, by='species', sharex=True)
# %% Histogramm mit allen Features
df.hist(layout=(1, 4))
# %% Histogramm mit allen Features, gruppiert nach Spezies
X = df.drop(columns='species')
y = df['species']
fig, axs = plt.subplots(1, 4)
for c in y.unique():
axs = X[y == c].hist(ax=axs, alpha=0.5)
# %% Kernel Density Estimation (KDE)
plt.figure()
sns.kdeplot(data=df, x='petal_length', fill=True)
plt.figure()
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True, alpha=0.5)
fig, axs = plt.subplots(1, 4, figsize=(10, 3), constrained_layout=True)
for ax, col in zip(axs, df.columns[:-1]):
sns.kdeplot(data=df, x=col, hue='species', fill=True, alpha=0.5, ax=ax)
if col != 'petal_width':
ax.get_legend().set_visible(False)
# %% Boxplot
df.boxplot(column='petal_length', by='species')
......@@ -55,7 +90,6 @@ pd.plotting.boxplot(df, by='species', ax=axs) # übergebe axs
fig.tight_layout()
# %% Violinenplot
import seaborn as sns
sns.violinplot(hue='species', y='petal_length', data=df)
......
%% Cell type:markdown id: tags:
# Code zu Folien
Dieses Skript bzw. Jupyter-Notebook enthält den Code, der auch auf den Folien "Pandas & Seaborn" enthalten ist. Zum Vorbereiten, Mitmachen oder Nacharbeiten.
%% Cell type:code id: tags:
```
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
```
%% Cell type:code id: tags:
```
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
df = pd.read_csv(url)
# offline-Alternative:
# from sklearn.datasets import load_iris
# df = pd.concat(load_iris(return_X_y=True, as_frame=True), axis='columns')
# df.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
# df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
df['species'] = df['species'].astype('category')
df
```
%% Cell type:code id: tags:
```
print(df.shape)
print(df.columns)
print(df.dtypes)
print(df.index)
df.info()
```
%% Cell type:code id: tags:
```
display(df.describe())
display(df.describe(exclude='number'))
```
%% Cell type:code id: tags:
```
counts = df['species'].value_counts()
display(counts)
counts.plot.pie(startangle=60, autopct='{:.2f}%'.format)
plt.ylabel('species')
```
%% Cell type:code id: tags:
```
col = ['petal_length']
df.plot.hist(column=col)
df.plot.hist(column=col, range=(1, 7), bins=12)
df.plot.hist(column=col, range=(1, 7), bins=12, by='species', sharex=True)
```
%% Cell type:code id: tags:
```
df.hist(layout=(1, 4))
```
%% Cell type:code id: tags:
```
X = df.drop(columns='species')
y = df['species']
fig, axs = plt.subplots(1, 4)
for c in y.unique():
axs = X[y == c].hist(ax=axs, alpha=0.5)
```
%% Cell type:code id: tags:
```
plt.figure()
sns.kdeplot(data=df, x='petal_length', fill=True)
plt.figure()
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True, alpha=0.5)
fig, axs = plt.subplots(1, 4, figsize=(10, 3), constrained_layout=True)
for ax, col in zip(axs, df.columns[:-1]):
sns.kdeplot(data=df, x=col, hue='species', fill=True, alpha=0.5, ax=ax)
if col != 'petal_width':
ax.get_legend().set_visible(False)
```
%% Cell type:code id: tags:
```
df.boxplot(column='petal_length', by='species')
```
%% Cell type:code id: tags:
```
fig, axs = plt.subplots(2, 2, sharey=False) # y-Achsen unabhängig
pd.plotting.boxplot(df, by='species', ax=axs) # übergebe axs
[ax.set_xlabel('') for ax in axs.ravel()] # entferne x-Labels
fig.tight_layout()
```
%% Cell type:code id: tags:
```
import seaborn as sns
sns.violinplot(hue='species', y='petal_length', data=df)
```
%% Cell type:code id: tags:
```
df.plot.scatter(x='petal_length', y='petal_width', c='species', colormap='viridis', alpha=0.7)
```
%% Cell type:code id: tags:
```
sns.pairplot(df, hue='species', plot_kws={'alpha': 0.5})
```
%% Cell type:code id: tags:
```
pd.plotting.parallel_coordinates(df, 'species', colormap='viridis', alpha=.5)
```
%% Cell type:code id: tags:
```
from sklearn.preprocessing import minmax_scale
num_cols = df.columns.drop('species')
df_scaled = df.copy()
df_scaled[num_cols] = minmax_scale(df[num_cols])
pd.plotting.parallel_coordinates(df_scaled, 'species', colormap='viridis', alpha=.5)
```
%% Cell type:code id: tags:
```
import numpy as np
from matplotlib.path import Path
import matplotlib.patches as patches
ys = df.drop(columns='species')
ynames = ys.columns
ys = ys.to_numpy()
ymins = ys.min(axis=0)
ymaxs = ys.max(axis=0)
dys = ymaxs - ymins
ymins -= dys * 0.05 # add 5% padding below and above
ymaxs += dys * 0.05
# reverse axis 1 to have less crossings
# ymaxs[1], ymins[1] = ymins[1], ymaxs[1]
# dys = ymaxs - ymins
# transform all data to be compatible with the main axis
zs = np.zeros_like(ys)
zs[:, 0] = ys[:, 0]
zs[:, 1:] = (ys[:, 1:] - ymins[1:]) / dys[1:] * dys[0] + ymins[0]
fig, host = plt.subplots(figsize=(10, 4))
axes = [host] + [host.twinx() for i in range(ys.shape[1] - 1)]
for i, ax in enumerate(axes):
ax.set_ylim(ymins[i], ymaxs[i])
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
if ax != host:
ax.spines['left'].set_visible(False)
ax.yaxis.set_ticks_position('right')
ax.spines["right"].set_position(("axes", i / (ys.shape[1] - 1)))
host.set_xlim(0, ys.shape[1] - 1)
host.set_xticks(range(ys.shape[1]))
host.set_xticklabels(ynames, fontsize=14)
host.tick_params(axis='x', which='major', pad=7)
host.spines['right'].set_visible(False)
host.xaxis.tick_top()
# host.set_title('Parallel Coordinates Plot — Iris', fontsize=18, pad=12)
colors = plt.cm.viridis([0, 128, 255])
target_names = df['species'].unique()
target = df['species'].cat.codes
legend_handles = [None for _ in target_names]
for j in range(ys.shape[0]):
# create bezier curves
verts = list(zip([x for x in np.linspace(0, len(ys) - 1, len(ys) * 3 - 2, endpoint=True)],
np.repeat(zs[j, :], 3)[1:-1]))
codes = [Path.MOVETO] + [Path.CURVE4 for _ in range(len(verts) - 1)]
path = Path(verts, codes)
patch = patches.PathPatch(path, facecolor='none',
lw=2, alpha=0.5, edgecolor=colors[target[j]])
legend_handles[target[j]] = patch
host.add_patch(patch)
host.legend(legend_handles, target_names,
loc='lower center', bbox_to_anchor=(0.5, -0.18),
ncol=len(target_names), fancybox=True, shadow=True)
```
%% Cell type:code id: tags:
```
import plotly.express as px
# fig = px.parallel_coordinates(df, color="species", labels={'species': tuple('ABC')})
fig = px.parallel_coordinates(df, color=df["species"].cat.codes)
fig.data[0]['dimensions'][-1]['label'] = 'species'
fig.show()
```
%% Cell type:code id: tags:
```
cp = df.copy()
cp.loc[1, 'sepal_width'] = 1
cp.loc[0:2, 'petal_length'] = 2
cp.loc[0, 'sepal_width':'petal_width'] = 3
cp.loc[1:, 'sepal_length'] = 4
cp.loc[:2, :'sepal_width'] = 5
cp.loc[:49, :].to_csv('iris-setosa.csv')
cp
```
%% Cell type:code id: tags:
```
display(df.loc[[0, 149, 2], 'petal_width'])
part = df.loc[[0, 149, 2], ['petal_width', 'sepal_width']]
display(part)
```
%% Cell type:code id: tags:
```
display(part.iloc[1, -1])
display(part.iloc[:2, -1])
display(part.iloc[[0, 1], [0, 1]])
```
%% Cell type:code id: tags:
```
pw = part.loc[:, 'petal_width'] <= 1
sw = part.loc[:, 'sepal_width'] < 3.5
display(pw)
display(sw)
display(~sw)
display(part.loc[pw & sw])
display(part.loc[pw | ~sw])
display(part.loc[pw ^ sw])
```
%% Cell type:code id: tags:
```
display(part.drop(index=149, columns='petal_width'))
display(part.drop(index=[149, 0]))
```
%% Cell type:code id: tags:
```
part.loc[3] = [2, 6]
display(part)
part.loc[:, 'weight'] = [1, 2, 3, 4]
display(part)
```
%% Cell type:code id: tags:
```
a = part.drop(index=3)
b = df.loc[:2, ['petal_length', 'petal_width']]
display(a)
display(b)
display(pd.concat((a, b), axis='columns'))
display(pd.concat((a, b), axis='index'))
```
%% Cell type:code id: tags:
```
df = pd.DataFrame({'name': ['Paul', 'John', 'Bill'], 'type': ['student', 'student', 'teacher']})
# kein Effekt, denn name wird name zugewiesen und type wird type zugewiesen
df.loc[:, ['name', 'type']] = df.loc[:, ['type', 'name']]
print(df)
print()
# kein Effekt, denn name wird name zugewiesen und type wird type zugewiesen
df.loc[:, ['name', 'type']] = df[['type', 'name']]
print(df)
print()
# tauscht Werte, weil __index__ (also []) kein Alignment hat
df[['name', 'type']] = df[['type', 'name']]
print(df)
print()
# tauscht Werte, weil auf der rechten Seite ein Numpy-Array steht.
df.loc[:, ['name', 'type']] = df[['type', 'name']].to_numpy()
print(df)
print()
# spaltenweise Zuweisung (ohne extra Klammern) ergibt Series, also werden keine Spalten aligned
temp = df['name'].copy() # kopiere die Werte, damit sie im nächsten Schritt nicht überschrieben werden
df.loc[:, 'name'] = df['type'] # tauscht Werte, weil es hier keine Spalten zu alignen gibt (sondern nur Zeilen, die aber zueinander passen) ...
df.loc[:, 'type'] = temp # ... auf der rechten und linken Seite steht jeweils eine pd.Series
print(df)
print()
# wegen Alignment hat Zeile 0 keinen Partner und bekommt NaN. Zeile 1 bekommt Zeile 1 zugewiesen.
df.loc[[0]] = df.loc[[1, 2]]
print(df)
print()
# wegen Alignment hat Zeile 0 keinen Partner und bekommt NaN. Zeile 1 bekommt Zeile 1 zugewiesen.
df.loc[[0, 1]] = df.loc[[1, 2]].to_numpy()
print(df)
print()
# wegen Alignment hat Spalte name keinen Partner und bekommt NaN.
df.loc[:, ['name']] = df[['type']]
print(df)
print()
# hier findet kein Alignment der Spalten statt, weil auf der rechten Seite nur eine pd.Series steht, es also keine Spalten zu alignen gibt
df.loc[:, ['name']] = df['type']
print(df)
print()
```
%% Cell type:code id: tags:
```
df['species']
df['species'].info()
```
%% Cell type:code id: tags:
```
X = df.drop(columns='species')
y = df['species']
display(X.mean())
display(y.value_counts())
```
%% Cell type:code id: tags:
```
species_means = X.groupby(y).mean()
display(species_means)
diff = species_means - [6, 3, 2, 0.5]
(diff**2).sum(axis='columns')
```
......
......@@ -6,6 +6,7 @@
# %% import Pandas
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
......@@ -44,6 +45,40 @@ counts.plot.pie(startangle=60, autopct='{:.2f}%'.format)
plt.ylabel('species')
# %% Histogramm mit einem Feature
col = ['petal_length']
df.plot.hist(column=col)
df.plot.hist(column=col, range=(1, 7), bins=12)
df.plot.hist(column=col, range=(1, 7), bins=12, by='species', sharex=True)
# %% Histogramm mit allen Features
df.hist(layout=(1, 4))
# %% Histogramm mit allen Features, gruppiert nach Spezies
X = df.drop(columns='species')
y = df['species']
fig, axs = plt.subplots(1, 4)
for c in y.unique():
axs = X[y == c].hist(ax=axs, alpha=0.5)
# %% Kernel Density Estimation (KDE)
plt.figure()
sns.kdeplot(data=df, x='petal_length', fill=True)
plt.figure()
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True, alpha=0.5)
fig, axs = plt.subplots(1, 4, figsize=(10, 3), constrained_layout=True)
for ax, col in zip(axs, df.columns[:-1]):
sns.kdeplot(data=df, x=col, hue='species', fill=True, alpha=0.5, ax=ax)
if col != 'petal_width':
ax.get_legend().set_visible(False)
# %% Boxplot
df.boxplot(column='petal_length', by='species')
......@@ -55,7 +90,6 @@ pd.plotting.boxplot(df, by='species', ax=axs) # übergebe axs
fig.tight_layout()
# %% Violinenplot
import seaborn as sns
sns.violinplot(hue='species', y='petal_length', data=df)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment