When it comes to tabular data, one of the best libraries to choose is `Pandas` (for an intro to `Pandas` see [this tutorial](https://git.fmrib.ox.ac.uk/fsl/win-pytreat/-/blob/fsleyes_branch/applications/pandas/pandas.ipynb)).

`Seaborn` is a visualisation library built on top of `matplotlib` and provides a convenient user interface to produce various types of plots.

When it comes to tabular data, one of the best libraries to choose is `pandas` (for an intro to `pandas` see [this tutorial](https://git.fmrib.ox.ac.uk/fsl/win-pytreat/-/blob/fsleyes_branch/applications/pandas/pandas.ipynb)).

`seaborn` is a visualisation library built on top of `matplotlib` and provides a convenient user interface to produce various types of plots from `pandas` dataframes.

This tutorial relies heavily on the materials provided in the [`seaborn` documentation](https://seaborn.pydata.org/examples/index.html).

%% Cell type:code id:suspected-worthy tags:

...

...

@@ -47,11 +48,23 @@

penguins

```

%% Cell type:markdown id:opposite-encounter tags:

Now let's see how the distribution of bill length vs depth look like. To do this, we have to pass these parameter names to the `x` and `y` axes.

`pandas` itself is able to produce different plots, by assigning the column names to the horizontal and vertical axes. This makes plotting the data stored in the table easier by using the human-readable strings of column names instead of lazily chosen variable names. Now let's see how the distribution of bill length vs depth look like as a scatter plot.

As noted in the beginning, `seaborn` is built on top of `pandas` and `matplotlib`, so everything that is acheivable via `seaborn` is achievable using a -- not so straightforward -- combination of the other two. But the magic of `seaborn` is that it makes the job of producing various publication-quality figures magnitudes of orders easier.

Let's start by plotting the same scatterplot, but this time via `seaborn`. To do this, we have to pass the names of columns of interest to the `x` and `y` parameters corresponding to the horizontal and vertical axes.

Let's open a large paranthesis here and explore some of the parameters that control figure aesthetics. We will later return to explore different plot types.

Seaborn comes with a couple of predefined themes; `darkgrid`, `whitegrid`, `dark`, `white`, `ticks`.

Let's make the figure above a bit fancier

%% Cell type:code id:herbal-combine tags:

``` python

sns.set_style('whitegrid')# which means white background, and grids on

```

%% Cell type:code id:shaped-clinton tags:

``` python

sns.set_style('whitegrid')# which means white background, and grids on

g.set(xlabel='Snoot length (mm)',ylabel='Snoot depth (mm)',title='Snoot depth vs length')

sns.despine()

```

%% Cell type:markdown id:textile-south tags:

One of the handy features of `seaborn` is that it can automatically adjust your figure properties according to the _context_ it is going to be used: `notebook`(default), `paper`, `talk`, and `poster`.

It seems that there are separate clusters in the data. A reasonable guess could be that the clusters correspond to different penguin species. We can color each dot based on a categorical variable (e.g., species) using the `hue` parameter.

It seems that there are separate clusters in the data. A reasonable guess could be that the clusters correspond to different penguin species. To test this, we can color each dot based on a categorical variable (e.g., species) using the `hue` parameter.

%% Cell type:code id:designed-angle tags:

``` python

sns.set_context('notebook')# set the context used for the subsequecnt plots

g.set(xlabel='Snoot length (mm)',ylabel='Snoot depth (mm)',title='Snoot depth vs length')

sns.despine()

```

...

...

@@ -140,11 +165,11 @@

%% Cell type:markdown id:parental-dispatch tags:

### Linear regression

<aid='scatter'></a>

There also seems to be a linear dependece between the two parameters, in each species. A linear fit to the data can be easily plotted by using `lmplot` which is a convenient shortcut to `scatterplot` with extra features for linear regression.

There also seems to be a linear dependece between the two parameters, separately in each species. A linear fit to the data can be easily plotted by using `lmplot` which is a convenient shortcut to `scatterplot` with extra features for linear regression.

However, it seems that the data could be better delineated in log scale... but `jointplot` is not flexible enough to do so. The solution is to go one level higher and build the figure we need using `JointGrid`.

Seaborn can also plot the marginal distributions, cool!

To do this, you should first create a `JointGrid` object that consists of a _joint_ axis and two _marginal_ axes, containing the joint distribution and the two marginal distributions.

`JointGrid` creates a _joint axis_ that hosts the jont distribution of the two variables and two _marginal axes_ that hosts the two marginal distributions. Each of these axes can show almost any of the plots available in `seaborn`, and they provide access to many detailed plot tweaks.

%% Cell type:code id:realistic-batman tags:

``` python

# define the JointGrid, and the data corresponding to each axis

# plot the joint histograms, overlayed with kernel density estimates

g.plot_marginals(sns.histplot,kde=True)

```

%% Cell type:markdown id:restricted-damages tags:

---

## Pairwise relationships

<aid='pair'></a>

`seaborn` also provides a convenient tool to have an overview of the relationships among each pair of the columns in a large table using `pairplot`. This could hint to potential dependencies among variables.

Lets see the overview of the pairwise relationships in the 🐧 dataset.

%% Cell type:code id:boolean-mineral tags:

``` python

sns.pairplot(penguins)

```

%% Cell type:markdown id:pressed-clone tags:

By default, scatterplots are used to show the distribution of each pair, and histograms of marginal distributions are shown on the diagonal. Note, however, that the upper-diagonal, lower-diagonal, and diagonal plots can be modified independently. For instance, we can plot the linear regression lines in the upper-diagonal plots using `regplot`, marginal histograms on the diagonals using `hist`, and kernel density estimates in the lowe-diagonal plots using `kdeplot`.