Plotting Data With Seaborn and Pandas
Tags: datascience, pandas, datavis, python
Published: 2019-06-26 20:48:13

---
title: Plotting Data With Seaborn and Pandas
published: true
tags: Data Science,Pandas,Data Vis,Python
canonical_url: https://hackersandslackers.com/plotting-data-with-seaborn-and-pandas/
---

 ![Plotting Data With Seaborn and Pandas](https://hackersandslackers.com/content/images/2019/04/seaborn-2-1.jpg)

There are plenty of good libraries for charting data in Python, perhaps _too_ many. Plotly is great, but a limit of 25 free charts is hardly a starting point. Sure, there's Matplotlib, but surely we find something a little less... well, lame. Where are all the simple-yet-powerful chart libraries at?

As you’ve probably guessed, this is where Seaborn comes in. Seaborn isn’t a third-party library, so you can get started without creating user accounts or worrying about API limits, etc. Seaborn is also built on top of Matplotlib, making it the logical next step up for anybody wanting some firepower from their charts.

We’ll explore Seaborn by charting some data ourselves. We'll walk through the process of preparing data for charting, plotting said charts and exploring the available functionality along the way. This tutorial assumes you have a working knowledge of Pandas, and access to a Jupyter notebook interface.

## Preparing Data in Pandas

Firs thing's first, we're going to need some data. To keep our focus on charting as opposed to complicated data cleaning, I'm going to use the most straightforward kind data set known to mankind: weather. As an added bonus, this will allows us to celebrate our inevitable impending doom as the world warms over 3 degrees Celsius on average in the years to come. The data set we'll be using is [Kaggle's Historial Hourly Weather Data](https://www.kaggle.com/selfishgene/historical-hourly-weather-data).

With these CSVs saved locally, we can get started inspecting our data:

<!--kg-card-begin: markdown-->
```python
import pandas as pd
import seaborn as sns
from matplotlib import pyplot

temperature_df = pd.read_csv('temperature.csv')

print(temperature_df.head(5))
print(temperature_df.tail(5))

```
<!--kg-card-end: markdown-->

This gives us the following output:

<!--kg-card-begin: html--><style>
    thead tr td:first-child {
       min-width: 190px;
    }
    tbody tr td:first-child {
       min-width: 190px;
    }
</style>

| | date | New York |
| --- | --- | --- |
| 24 | 2012-10-02 | 289.99 |
| 48 | 2012-10-03 | 290.37 |
| 72 | 2012-10-04 | 290.84 |
| 96 | 2012-10-05 | 293.18 |
| 120 | 2012-10-06 | 288.24 |
| 8640 | 2013-09-26 | 286.22 |
| 8664 | 2013-09-27 | 285.37 |
| 8688 | 2013-09-28 | 286.65 |
| 8712 | 2013-09-29 | 286.290 |
| 8736 | 2013-09-30 | 283.435 |

<!--kg-card-end: html-->

This tells us a few things:

- The extent of this data set begins on October 1st, 2012 and ends on on November 29th, 2017.
- Not all cities have sufficient data which begins and ends on these dates.
- Data has been taken at 1-hour intervals, 24 times per day.
- Temperatures are in Kelvin: the world's most useless unit of measurement.

### Remove Extraneous Data

24 readings per day is a lot. Creating charts can take a significant amount of system resources (and time), which makes 24 separate temperatures every day ludicrous. Let's save ourselves 23/24ths of this headache by taking our recorded temperatures down to one reading per day.

We'll modify our DataFrame to only include one out of every 24 rows:

<!--kg-card-begin: markdown-->
```
modified_df = temperature_df.iloc[::24]

```
<!--kg-card-end: markdown-->
### Take A Sample Size

We'll only want to chart the values for one location to start, so let's start with New York.

It could also be interesting to map this data year-over-year to observe any changes. To do this, we'll reduce our data set to only include the first year of readings.

Lastly, we'll want to remove those empty cells we noticed from this incomplete data set. All the above gives us the following:

<!--kg-card-begin: markdown-->
```python
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

temperature_df = pd.read_csv('temperature.csv')
nyc_df = temperature_df[['datetime','New York']]
nyc_df = nyc_df.iloc[::24]
nyc_df.dropna(how='any', inplace=True)

print(nyc_df.head(5))
print(nyc_df.tail(5))

```
<!--kg-card-end: markdown-->

The resulting snippet gives us one recorded temperature per day in the first year of results. The output should look like this:

<!--kg-card-begin: html--><style>
    thead tr td:first-child {
       min-width: 190px;
    }
    tbody tr td:first-child {
       min-width: 190px;
    }
</style>

| | date | New York |
| --- | --- | --- |
| 24 | 2012-10-02 | 289.99 |
| 48 | 2012-10-03 | 290.37 |
| 72 | 2012-10-04 | 290.84 |
| 96 | 2012-10-05 | 293.18 |
| 120 | 2012-10-06 | 288.24 |
| 8640 | 2013-09-26 | 286.22 |
| 8664 | 2013-09-27 | 285.37 |
| 8688 | 2013-09-28 | 286.65 |
| 8712 | 2013-09-29 | 286.290 |
| 8736 | 2013-09-30 | 283.435 |

<!--kg-card-end: html-->
### Fixing Our Temperatures

We've got to do something about this Kelvin situation. The last time I measured anything in degrees Kelvin was while monitoring my reactor heat in **Mech Warrior 2**. Don't try and find that game, it's a relic from the 90's.

A quick Google search reveals the formula for converting Kelvin to Fahrenheit:

<!--kg-card-begin: markdown-->
```
(x − 273.15) × 9/5 + 32

```
<!--kg-card-end: markdown-->

This calls for a lambda function!

<!--kg-card-begin: markdown-->
```python
nyc_df['temp'] = nyc_df['temp'].apply(lambda x: (x-273.15) * 9/5 + 32)

```
<!--kg-card-end: markdown-->

Checking our work with `print(nyc_df.head(5))`:

<!--kg-card-begin: html-->

| date | temperature | index |
| --- | --- | --- |
| 2012-10-02 | 62.584651 | 0 |
| 2012-10-03 | 63.266 | 1 |
| 2012-10-04 | 64.112 | 2 |
| 2012-10-05 | 68.324 | 3 |
| 2012-10-06 | 59.432 | 4 |

<!--kg-card-end: html-->
### Adjusting Our Datatypes

We loaded our data from a CSV, so we _know_ our data types are going to be trash. Pandas notoriously stores data types from CSVs as objects when it doesn't know what's up. Quickly running `print(nyc_df.info())` reveals this:

<!--kg-card-begin: markdown-->
```
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1852 entries, 24 to 44448
Data columns (total 2 columns):
date 1852 non-null object
temp 1852 non-null float64
dtypes: float64(1), object(1)
memory usage: 43.4+ KB
None

```
<!--kg-card-end: markdown-->

"Object" is a fancy Pandas word for "uselessly broad classification of data type." Pandas sees the special characters in this column's data, thus immediately surrenders any attempt to logically parse said data. Let's fix this:

<!--kg-card-begin: markdown-->
```python
nyc_df['date'] = pd.to_datetime(nyc_df['date'])

```
<!--kg-card-end: markdown-->

.info() should now display the "date" column as being a "datetime" data type:

<!--kg-card-begin: markdown-->
```
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1852 entries, 24 to 44448
Data columns (total 2 columns):
date 1852 non-null datetime64[ns]
temp 1852 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage:43.4 KB
None

```
<!--kg-card-end: markdown-->
### Seaborn-Friendly Data Formatting

Consider the chart we're about to make for a moment: we're looking to make a multi-line chart on a single plot, where we overlay temperature readings atop each other, year-over-year. Despite mapping multiple lines, Seaborn plots will only accept a DataFrame which has a single column for all X values, and a single column for all Y values. This means that despite being multiple lines, all of our lines' values will live in a single massive column. Because of this, we need to somehow group cells in this column as though to say "these values belong to line 1, those values belong to line 1".

We can accomplish this via a third column. This column serves as a "label" which will group all values of the same label together (ie: create a single line). We're creating one plot per year, so this is actually quite easy:

<!--kg-card-begin: markdown-->
```python
nyc_df['year'] = nyc_df['date'].dt.year
```
<!--kg-card-end: markdown-->

It's that simple! We need to do t his once more for the _day of the year_ to represent our X values:

<!--kg-card-begin: markdown-->
```python
nyc_df['day'] = nyc_df['date'].dt.dayofyear
```
<!--kg-card-end: markdown-->

Checking the output:

<!--kg-card-begin: markdown-->
```
                   date temp year day 
0 2012-10-02 12:00:00 62.3147 2012 276 
1 2012-10-03 12:00:00 62.9960 2012 277 
2 2012-10-04 12:00:00 63.8420 2012 278 
3 2012-10-05 12:00:00 68.0540 2012 279 
4 2012-10-06 12:00:00 59.1620 2012 280
```
<!--kg-card-end: markdown-->
### Housecleaning

We just did a whole bunch, but we've made a bit of a mess in the process.

For one, we never modified our column names. Why have a column named **New York** in a data set which is already named as such? How would anybody know what the numbers in this column represent, exactly? Let's adjust:

<!--kg-card-begin: markdown-->
```python
nyc_df.columns = ['date','temperature']
```
<!--kg-card-end: markdown-->

Our row indexes need to be fixed after dropping all those empty rows earlier. Let's fix this:

<!--kg-card-begin: markdown-->
```python
nyc_df.reset_index(inplace=True)
```
<!--kg-card-end: markdown-->

Whew, that was a lot! Let's recap everything we've done so far:

<!--kg-card-begin: markdown-->
```python
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

# Load Data From CSV
temperature_df = pd.read_csv('temperature.csv')

# Get NYC temperatures daily
nyc_df = temperature_df[['datetime','New York']]
nyc_df = nyc_df.iloc[::24]
nyc_df.dropna(how='any', inplace=True)

# Convert temperature to Farenheight
nyc_df['New York'] = nyc_df['New York'].apply(lambda x: (x-273.15) * 9/5 + 32)

# Set X axis, group Y axis
nyc_df['date'] = pd.to_datetime(nyc_df['date'])
nyc_df['year'] = nyc_df['date'].dt.year
nyc_df['day'] = nyc_df['date'].dt.dayofyear

# Cleanup
nyc_df.columns = ['date','temperature']
nyc_df.reset_index(inplace=True)
```
<!--kg-card-end: markdown-->
## Chart Preparation

The first type of chart we'll be looking at will be a line chart, showing temperature change over time. Before mapping that data, we must first set the stage. Without setting the proper metadata for our chart, it will default to being an ugly, 5x5 square without a title.

### Showing Charts Inline in Jupyter

Before we can see any charts, we need to explicitly configure our notebook to show inline charts. This is accomplished in Jupyter with a "[magic function](https://ipython.readthedocs.io/en/stable/interactive/magics.html)". Scroll back to the start of your notebook, and make sure `%matplotlib inline` is present from the beginning:

<!--kg-card-begin: markdown-->
```python
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
%matplotlib inline
```
<!--kg-card-end: markdown-->
### Setting Plot Size 

If you're familiar with Matplotlib, this next part should look familiar:

<!--kg-card-begin: markdown-->
```python
plt.figure(figsize=(15, 7))
```
<!--kg-card-end: markdown-->

Indeed, we're acting on `plt`, which is the alias we gave `pyplot` (an import from the Matplotlib library). The above sets the dimensions of a chart: 15x7 inches. Because Seaborn runs on Matplotlib at its core, we can modify our chart with the same syntax as modifying Matplotlib charts.

### Setting Our Chart Colors

But what about colors? Isn't the "pretty" aspect of Seaborn the whole reason we're using it? Indeed it is, friend:

<!--kg-card-begin: markdown-->
```python
sns.palplot(sns.color_palette("husl", 8))
```
<!--kg-card-end: markdown-->

...And the output:

<!--kg-card-begin: image-->

![Plotting Data With Seaborn and Pandas](https://hackersandslackers.com/content/images/2019/04/download-1.png)<figcaption>Thumbnails for the "husl" Seaborn palette.</figcaption>

<!--kg-card-end: image-->

Wait, what did we just do? We set a new color pallet for our chart, using the built-in "husl" palette built in the Seaborn. There are plenty of other ways to control your color palette, which is poorly explained in their documentation [here](https://seaborn.pydata.org/tutorial/color_palettes.html).

ADD STYLE=TICKS HERE.

### Naming Our Chart

Another example:

<!--kg-card-begin: markdown-->
```python
plt.set_title('NYC Weather Over Time')
```
<!--kg-card-end: markdown-->

We've now titled our chart **NYC Weather Over Time.** The two lines of code above combined result in this output:

<!--kg-card-begin: image-->

![Plotting Data With Seaborn and Pandas](https://hackersandslackers.com/content/images/2019/04/plot.png)<figcaption>Behold: A blank canvas.</figcaption>

<!--kg-card-end: image-->

We've now set the style of our chart, set the size dimensions, and given it a name. We're just about ready for business.

## Plotting Line Charts

Now for the good stuff: creating charts! In Seaborn, a plot is created by using the `sns.plottype()` syntax, where `plottype()` is to be substituted with the type of chart we want to see. We're plotting a line chart, so we'll use `sns.lineplot()`:

<!--kg-card-begin: markdown-->
```python
nyc_chart = sns.lineplot(x="day",
                         y="temp",
                         hue='year',
                         data=nyc_df
                         ).set_title('NYC Weather Over Time')
plt.show()
```
<!--kg-card-end: markdown-->

Take note of our passed arguments here:

- `data` is the Pandas DataFrame containing our chart's data.
- `x` and `y` are the columns in our DataFrame which should be assigned to the **x** and **y** axises, respectively.
- `hue` is the label by which to group values of the Y axis.

Of course, `lineplot()` accepts many more arguments we haven't touched on. For example:

- `ax` accepts a Matplotlib 'plot' object, like the one we created containing our chart metadata. We didn't have to pass this because Seaborn automatically inherits what we save to our `plt` variable by default. If we had multiple plots, this would be useful.
- `size` allows us to change line width based on a variable.
- `legend` provides three different options for how to display the legend.

Full documentation of all arguments accepted by `lineplot()` can be found [here](https://seaborn.pydata.org/generated/seaborn.lineplot.html). Enough chatter, let's see what our chart looks like!

<!--kg-card-begin: image-->

![Plotting Data With Seaborn and Pandas](https://hackersandslackers.com/content/images/2019/04/nyc_temperature_over_time.png)<figcaption>She's a beaut.</figcaption>

<!--kg-card-end: image-->

You must be Poseidon 'cuz you're looking like the king of the sea right now.

### Acheivement Unlocked

I commend your patience for sitting through yet another wordy about cleaning data. Unfortunately, 3 years isn't long enough of a time period to visually demonstrate that climate change exists: the world's temperature is projected to rise an average 2-3 degrees celsius over the span of roughly a couple hundred years, so our chart isn't actually very useful.

Seaborn has plenty of more chart types than just simple line plots. Hopefully, you're feeling comfortable enough to start poking around [the documentation](https://seaborn.pydata.org/) and spread those wings a bit.