Deep dive into seaborn : meet the datasets
By: Yoann Mocquin
Tags: Data, Plot, Python, Seaborn

To demonstrate seaborn and its powerfull plotting functions, it is convenient to have sample datasets quickly available to plot. For this, seaborn provides a simple interface to load dataset into pandas dataframes.

List datasets

As of today (16/12/2020, seaborn v0.11), seaborn provides 18 datasets that are stored in a dedicated github repository :

In addition to browsing the seaborn-data repo, you can get the list of available datasets directly from python using the get_datasets_names function. When called, it will return a list of strings containing the names of the datasets:

Each item in this list maps to a dataset on the repo : for example, the dataset named `iris` maps to the `iris.csv` file.

Load datasets

Now that you know what are the datasets available, let’s load them !

For this, simply use the sns.load_dataset method on a dataset. For example sns.load_dataset('iris’) will load the iris dataset into a pandas Dataframe. From then, we can start playing with the dataset :

Caching the datasets

As the seaborn datasets are stored online, you need an internet connection both to get the list of available datasets (using get_datasets_names ) and load the datasets (using load_dataset ).

Well, at least the first time.

In order to avoid having to download the same data over and over, seaborn will by default store the datasets (or “cache”) in a data_home folder : by default seaborn will store the datasets in ~/seaborn-data . Alternatively, seaborn will try to store data in the path given by the SEABORN_DATA environment variable if defined.

All this happens because the load_dataset function is called with cache=True by default. This way, when a dataset has been downloaded from the online repo with load_dataset , seaborn will save the dataset in this folder, and later will directly load the data from this folder.

Another possibility to access the datasets is to manually download them from the github repo, and store them in a folder of your choice. You can then use the data_home parameter of load_dataset . For example :

To locate the default data_home folder used by seaborn, use the get_data_home function :

Extending your local seaborn data home

Of course, you can also add any other dataset in your data_home folder, and then use the seaborn interface to load them. To match specific datasets (in terms of separator, indexes), you can pass kwargs to load_dataset that will be used by pandas to read the data.

Wrap up

In this article, we saw :

where the datasets are stored online : in a dedicated github repo at https://github.com/mwaskom/seaborn-data

how to list the available datasets : using sns.get_datasets_names()

how to load the dataset : using sns.load_dataset

where the datasets are cached : by default in ~/seaborn-data or the path given by the SEABORN_DATA environment variable. This happens because load_dataset is called with cache=True by default.

or the path given by the environment variable. This happens because is called with by default. how you can define and use a local dataset folder, called data_home in seaborn : place your data in a folder and use sns.load_dataset with a data_home argument.

If you liked this, checkout my article about seaborn’s palettes :