# Plotting Data With Seaborn and Pandas

Plotting Data With Seaborn and PandasCreate beautiful data visualizations out-of-the-box with Python’s Seaborn.

Todd BirchardBlockedUnblockFollowFollowingApr 29There 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 PandasFirst 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.

With these CSVs saved locally, we can get started inspecting our data:import pandas as pdimport seaborn as snsfrom matplotlib import pyplottemperature_df = pd.

csv')print(temperature_df.

tail(5))This gives us the following output: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 Data24 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:modified_df = temperature_df.

iloc[::24]Take A Sample SizeWe’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:import pandas as pdimport seaborn as snsfrom matplotlib import pyplot as plttemperature_df = pd.

csv')nyc_df = temperature_df[['datetime','New York']]nyc_df = nyc_df.

iloc[::24]nyc_df.

dropna(how='any', inplace=True)print(nyc_df.

tail(5))The resulting snippet gives us one recorded temperature per day in the first year of results.

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:(x − 273.

15) × 9/5 + 32This calls for a lambda function!nyc_df['temp'] = nyc_df['temp'].

apply(lambda x: (x-273.

15) * 9/5 + 32)Checking our work with print(nyc_df.

head(5)):Adjusting Our DatatypesWe 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:<class 'pandas.

core.

frame.

DataFrame'>Int64Index: 1852 entries, 24 to 44448Data columns (total 2 columns):date 1852 non-null objecttemp 1852 non-null float64dtypes: float64(1), object(1)memory usage: 43.

4+ KBNone“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:nyc_df['date'] = pd.

to_datetime(nyc_df['date']).

info() should now display the “date” column as being a “datetime” data type:<class 'pandas.

core.

frame.

DataFrame'>Int64Index: 1852 entries, 24 to 44448Data columns (total 2 columns):date 1852 non-null datetime64[ns]temp 1852 non-null float64dtypes: datetime64[ns](1), float64(1)memory usage:43.

4 KBNoneSeaborn-Friendly Data FormattingConsider 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:nyc_df['year'] = nyc_df['date'].

dt.

yearIt’s that simple!.We need to do this once more for the day of the year to represent our X values:nyc_df['day'] = nyc_df['date'].

dt.

dayofyearChecking the output: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 280HousecleaningWe 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:nyc_df.

columns = ['date','temperature']Our row indexes need to be fixed after dropping all those empty rows earlier.

Let’s fix this:nyc_df.

reset_index(inplace=True)Whew, that was a lot!.Let’s recap everything we’ve done so far:import pandas as pdimport seaborn as snsfrom matplotlib import pyplot as plt# Load Data From CSVtemperature_df = pd.

csv')# Get NYC temperatures dailynyc_df = temperature_df[['datetime','New York']]nyc_df = nyc_df.

iloc[::24]nyc_df.

dropna(how='any', inplace=True)# Convert temperature to Farenheightnyc_df['New York'] = nyc_df['New York'].

apply(lambda x: (x-273.

15) * 9/5 + 32)# Set X axis, group Y axisnyc_df['date'] = pd.

to_datetime(nyc_df['date'])nyc_df['year'] = nyc_df['date'].

dt.

yearnyc_df['day'] = nyc_df['date'].

dt.

dayofyear# Cleanupnyc_df.

columns = ['date','temperature']nyc_df.

reset_index(inplace=True)Chart PreparationThe 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, 5×5 square without a title.

Showing Charts Inline in JupyterBefore 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”.

Scroll back to the start of your notebook, and make sure %matplotlib inline is present from the beginning:import pandas as pd import seaborn as snsfrom matplotlib import pyplot as plt%matplotlib inlineSetting Plot SizeIf you’re familiar with Matplotlib, this next part should look familiar:plt.

figure(figsize=(15, 7))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: 15×7 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 ColorsBut what about colors?.Isn’t the “pretty” aspect of Seaborn the whole reason we’re using it?.Indeed it is, friend:sns.

palplot(sns.

color_palette("husl", 8))…And the output:Thumbnails for the “husl” Seaborn palette.

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.

Naming Our Chartplt.

set_title('NYC Weather Over Time')We’ve now titled our chart NYC Weather Over Time.

The two lines of code above combined result in this output:Behold: A blank canvas.

We’ve now set the style of our chart, set the size dimensions, and given it a name.

Plotting Line ChartsNow 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():nyc_chart = sns.

lineplot(x="day", y="temp", hue='year', data=nyc_df ).

set_title('NYC Weather Over Time')plt.

show()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.

Enough chatter, let's see what our chart looks like!She’s a beaut.

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

Achievement UnlockedI 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 and spread those wings a bit.

Originally published at https://hackersandslackers.

com on April 29, 2019.