Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Hackers And Slackers profile imageTodd Birchard
Todd Birchard forHackers And Slackers

Posted on • Originally published athackersandslackers.com on

     

Plotting Data With Seaborn and Pandas

Plotting Data With Seaborn and Pandas

There are plenty of good libraries for charting data in Python, perhapstoo 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 isKaggle's Historial Hourly Weather Data.

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

importpandasaspdimportseabornassnsfrommatplotlibimportpyplottemperature_df=pd.read_csv('temperature.csv')print(temperature_df.head(5))print(temperature_df.tail(5))
Enter fullscreen modeExit fullscreen mode

This gives us the following output:

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

dateNew York
242012-10-02289.99
482012-10-03290.37
722012-10-04290.84
962012-10-05293.18
1202012-10-06288.24
86402013-09-26286.22
86642013-09-27285.37
86882013-09-28286.65
87122013-09-29286.290
87362013-09-30283.435

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:

modified_df = temperature_df.iloc[::24]
Enter fullscreen modeExit fullscreen mode

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:

importpandasaspdimportseabornassnsfrommatplotlibimportpyplotasplttemperature_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))
Enter fullscreen modeExit fullscreen mode

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--><br> thead tr td:first-child {<br> min-width: 190px;<br> }<br> tbody tr td:first-child {<br> min-width: 190px;<br> }<br>

dateNew York
242012-10-02289.99
482012-10-03290.37
722012-10-04290.84
962012-10-05293.18
1202012-10-06288.24
86402013-09-26286.22
86642013-09-27285.37
86882013-09-28286.65
87122013-09-29286.290
87362013-09-30283.435

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 inMech 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 + 32
Enter fullscreen modeExit fullscreen mode

This calls for a lambda function!

nyc_df['temp']=nyc_df['temp'].apply(lambdax:(x-273.15)*9/5+32)
Enter fullscreen modeExit fullscreen mode

Checking our work withprint(nyc_df.head(5)):

datetemperatureindex
2012-10-0262.5846510
2012-10-0363.2661
2012-10-0464.1122
2012-10-0568.3243
2012-10-0659.4324

Adjusting Our Datatypes

We loaded our data from a CSV, so weknow 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 runningprint(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
Enter fullscreen modeExit fullscreen mode

"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'])
Enter fullscreen modeExit fullscreen mode

.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 KBNone
Enter fullscreen modeExit fullscreen mode

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:

nyc_df['year']=nyc_df['date'].dt.year
Enter fullscreen modeExit fullscreen mode

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

nyc_df['day']=nyc_df['date'].dt.dayofyear
Enter fullscreen modeExit fullscreen mode

Checking 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 280
Enter fullscreen modeExit fullscreen mode

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 namedNew 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']
Enter fullscreen modeExit fullscreen mode

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

nyc_df.reset_index(inplace=True)
Enter fullscreen modeExit fullscreen mode

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

importpandasaspdimportseabornassnsfrommatplotlibimportpyplotasplt# Load Data From CSVtemperature_df=pd.read_csv('temperature.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(lambdax:(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)
Enter fullscreen modeExit fullscreen mode

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". Scroll back to the start of your notebook, and make sure%matplotlib inline is present from the beginning:

importpandasaspdimportseabornassnsfrommatplotlibimportpyplotasplt%matplotlibinline
Enter fullscreen modeExit fullscreen mode

Setting Plot Size

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

plt.figure(figsize=(15,7))
Enter fullscreen modeExit fullscreen mode

Indeed, we're acting onplt, which is the alias we gavepyplot (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:

sns.palplot(sns.color_palette("husl",8))
Enter fullscreen modeExit fullscreen mode

...And the output:

Plotting Data With Seaborn and Pandas
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 documentationhere.

ADD STYLE=TICKS HERE.

Naming Our Chart

Another example:

plt.set_title('NYC Weather Over Time')
Enter fullscreen modeExit fullscreen mode

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

Plotting Data With Seaborn and Pandas
Behold: A blank canvas.

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 thesns.plottype() syntax, whereplottype() is to be substituted with the type of chart we want to see. We're plotting a line chart, so we'll usesns.lineplot():

nyc_chart=sns.lineplot(x="day",y="temp",hue='year',data=nyc_df).set_title('NYC Weather Over Time')plt.show()
Enter fullscreen modeExit fullscreen mode

Take note of our passed arguments here:

  • data is the Pandas DataFrame containing our chart's data.
  • x andy are the columns in our DataFrame which should be assigned to thex andy 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 ourplt 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 bylineplot() can be foundhere. Enough chatter, let's see what our chart looks like!

Plotting Data With Seaborn and Pandas
She's a beaut.

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 aroundthe documentation and spread those wings a bit.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Technology for Badasses.

A community of degenerates obsessed with data science, data engineering, and analysis. Tackling major issues with unconventional tutorials. Openly pushing a pro-robot agenda.

More fromHackers And Slackers

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp