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)).
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.
`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).
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:
%% Cell type:code id:suspected-worthy tags:
``` python
``` python
importseabornassns
importseabornassns
importpandasaspd
importpandasaspd
importmatplotlib.pyplotasplt
importmatplotlib.pyplotasplt
importnumpyasnp
importnumpyasnp
sns.set()
sns.set()
```
```
%% Cell type:markdown id:champion-answer tags:
%% Cell type:markdown id:champion-answer tags:
## Plotting relative distributions
## Plotting relative distributions
---
---
<aid='scatter'></a>
<aid='scatter'></a>
Seaborn library provides a couple of `pandas` datsets to explore various plot types. The one we load below is about penguins 🐧
Seaborn library provides a couple of `pandas` datsets to explore various plot types. The one we load below is about penguins 🐧
%% Cell type:code id:pressed-individual tags:
%% Cell type:code id:pressed-individual tags:
``` python
``` python
# Load the penguins dataset
# Load the penguins dataset
penguins=sns.load_dataset("penguins")
penguins=sns.load_dataset("penguins")
```
```
%% Cell type:code id:elder-corner tags:
%% Cell type:code id:elder-corner tags:
``` python
``` python
penguins
penguins
```
```
%% Cell type:markdown id:opposite-encounter tags:
%% Cell type:markdown id:opposite-encounter tags:
`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.
`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 orders of magnitudes easier.
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 orders of magnitude 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 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.
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`.
Seaborn comes with a couple of predefined themes; `darkgrid`, `whitegrid`, `dark`, `white`, `ticks`.
Let's make the figure above a bit fancier
Let's make the figure above a bit fancier
%% Cell type:code id:shaped-clinton tags:
%% Cell type:code id:shaped-clinton tags:
``` python
``` python
sns.set_style('whitegrid')# which means white background, and grids on
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')
g.set(xlabel='Snoot length (mm)',ylabel='Snoot depth (mm)',title='Snoot depth vs length')
sns.despine()
sns.despine()
```
```
%% Cell type:markdown id:textile-south tags:
%% 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`.
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. To test this, 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:
%% Cell type:code id:designed-angle tags:
``` python
``` python
sns.set_context('notebook')# set the context used for the subsequecnt plots
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')
g.set(xlabel='Snoot length (mm)',ylabel='Snoot depth (mm)',title='Snoot depth vs length')
sns.despine()
sns.despine()
```
```
%% Cell type:markdown id:fourth-southwest tags:
%% Cell type:markdown id:fourth-southwest tags:
The guess was correct!
The guess was correct!
---
---
%% Cell type:markdown id:parental-dispatch tags:
%% Cell type:markdown id:parental-dispatch tags:
### Linear regression
### Linear regression
<aid='scatter'></a>
<aid='scatter'></a>
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.
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.
The confidence bounds shown above are calculated based on standard deviation by default. Alternatively confidence interval can be used by specifying the confidence interval percentage
The confidence bounds shown above are calculated based on standard deviation by default. Alternatively confidence interval can be used by specifying the confidence interval percentage
g.fig.suptitle('Snoot depth vs length -- 80% CI',y=1.05)
g.fig.suptitle('Snoot depth vs length -- 80% CI',y=1.05)
```
```
%% Cell type:markdown id:employed-twenty tags:
%% Cell type:markdown id:employed-twenty tags:
---
---
## Data aggregation and uncertainty bounds
## Data aggregation and uncertainty bounds
<aid='line'></a>
<aid='line'></a>
In some datasets, repetitive measurements for example, there might be multiple values from one variable corresponding to each instance from the other variable. To explore an instance of such data, lets load the `fmri` dataset.
In some datasets, repetitive measurements for example, there might be multiple values from one variable corresponding to each instance from the other variable. To explore an instance of such data, lets load the `fmri` dataset.
By default, mean of the signal at each `x` instance is plotted. But any arbitrary function could also be used to aggregate the data. For instance, we could use the `median` function from `numpy` package to calculate the value corresponding to each timepoint.
By default, mean of the signal at each `x` instance is plotted. But any arbitrary function could also be used to aggregate the data. For instance, we could use the `median` function from `numpy` package to calculate the value corresponding to each timepoint.
Similar to any other plot type in `seaborn`, data with different semantics can be separately plotted by assigning them to `hue`, `col`, or `style` parameters. Let's separately plot the data for the _parietal_ and _frontal_ regions.
Similar to any other plot type in `seaborn`, data with different semantics can be separately plotted by assigning them to `hue`, `col`, or `style` parameters. Let's separately plot the data for the _parietal_ and _frontal_ regions.