"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)). \n",
"`Seaborn` is a visualisation library built on top of `matplotlib` and provides a convenient user interface to produce various types of plots. \n"
"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.\n",
"\n",
"Seaborn comes with a couple of predefined themes; `darkgrid`, `whitegrid`, `dark`, `white`, `ticks`. \n",
"Let's make the figure above a bit fancier"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "herbal-combine",
"metadata": {},
"outputs": [],
"source": [
"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')\n",
"sns.despine()"
]
},
{
"cell_type": "markdown",
"id": "fuzzy-welsh",
"metadata": {},
"source": [
"paranthesis closed.\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "architectural-corruption",
"metadata": {},
"source": [
"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."
"g.set(xlabel='Snoot length (mm)', ylabel='Snoot depth (mm)', title='Snoot depth vs length')\n",
"sns.despine()"
]
},
{
"cell_type": "markdown",
"id": "fourth-southwest",
"metadata": {},
"source": [
"The guess was correct!\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "parental-dispatch",
"metadata": {},
"source": [
"### Linear regression\n",
"<a id='scatter'></a>\n",
"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."
"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)"
]
},
{
"cell_type": "markdown",
"id": "employed-twenty",
"metadata": {},
"source": [
"---\n",
"\n",
"## Data aggregation and uncertainty bounds\n",
"<a id='line'></a>\n",
"\n",
"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."
"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."
"Seaborn can also plot the marginal distributions, cool!\n",
"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. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "realistic-batman",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"# define the JointGrid, and the data corresponding to each axis\n",
"# plot the joint histograms, overlayed with kernel density estimates\n",
"g.plot_marginals(sns.histplot, kde=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:caring-plate tags:
# Tabular data visualisation using Seaborn
---
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.