6 Simple Tips For Prettier and Customised Plots in Seaborn (Python) - by Zolzaya Luvsandorj - Toward

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

🔢 💡🐨🔑 📍

◼ ❕ 🌟 🏃💨 📊
Sign in Get started

WRITTEN BY

Zolzaya
Luvsandorj
Follow 513K Followers · Editors' Picks Features Explore Contribute About
Data Scientist |
Growth Mindset |
Math Lover |
Melbourne, AU |
https://zluvsand.gith This is your last free member-only story this month. Sign up for Medium and get an extra one
ub.io/

Follow

6 simple tips for prettier and


customised plots in Seaborn
206

(Python)
Beginner’s guide to easily personalise your plots

Zolzaya Luvsandorj Oct 6 · 6 min read

In this post, we will look at simple ways to customise your plots to make
them aesthetically more pleasing. I hope these simple tricks will help you
get better-looking plots and save you time from adjusting individual plots.

Photo by Kelli Tungay on Unsplash

Baseline plot
The scripts in this post are tested in Python 3.8.3 in Jupyter Notebook.

Let’s use Seaborn’s built-in dataset on penguins as our sample data:

# Import packages
import matplotlib.pyplot as plt
import seaborn as sns

# Import data
df = sns.load_dataset('penguins').rename(columns={'sex': 'gender'})
df

We will build a standard scatter plot with the default chart settings to use it
as our baseline:

# Plot
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')

We will see the journey of how this plot alters with each tip.

Tips
You will see that the Crst two tips are for individual plots whereas the
remaining four tips are for changing the default settings for all charts.

Tip #1: Semicolon


Did you notice the text output right above the chart in the previous plot? A
simple way to suppress this text output is to use ; at the end of your plot.

# Plot
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender');

By only adding ; at the end of our code, we get a cleaner output.

Tip #2: plt.figure()


Plots can often beneCt from resizing. If we wanted to resize, this is how we
would do it:

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender');

When we resized, the legend moved to the upper left corner. Let’s move the
legend outside the chart so that it doesn’t accidentally cover data points:

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1));

If you are wondering how to know what number combinations to use for
figsize() or bbox_to_anchor() , you will need to trial and error which

numbers work best for the plot.

Tip #3: sns.set_style()


This function helps to change the overall style of the plots if we don’t like
the default style. This includes things like the aesthetics of the axis colours
and background. Let’s change the style to whitegrid and see how the plot
appearance changes:

# Change default style


sns.set_style('whitegrid')

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1));

Here are some more other options to try out: 'darkgrid' , 'dark' and
'ticks' to Cnd the one you fancy more.

Tip #4: sns.set_context()


The label sizes look quite small in the previous plot. With
sns.set_context() , we could change the context parameters if we don’t like

the default settings. I use this function mainly to control the default font
size for labels in the plots. By changing the default, we can save time from
not having to tweak the font size for diNerent elements (e.g. axis label, title,
legend) of individual plots. Let’s change the context to 'talk' and look at
the plot again:

# Change default context


sns.set_context('talk')

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));

It’s more easily legible, isn’t it? Another option to try out is: 'poster' which
will increase the default size even more.

Tip #5: sns.set_palette()


If you ever want to customise the default colour palette to your preferred
colour combinations, this function comes in handy. We can use colourmaps
from Matplotlib. Here are the lists of Matplotlib colourmaps to choose from.
Let’s change the palette to 'rainbow' and look at the plot again:

# Change default palette


sns.set_palette('rainbow')

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));

If you can’t Cnd a Matplotlib colourmap that you like, you can hand pick
colours to create your own unique colour palette. One way to create your
own palette is to pass a list of colour names to the function like in the
example below. Here is the list of colour names.

# Change default palette


sns.set_palette(['green', 'purple', 'red'])

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));

If the colour names don’t quite capture what you are after, you can build
your own palette using hexadecimal colours to access a wider range of
options (over 16 million colours!). Here’s my favourite resource to Cnd a
custom colour palette in hexadecimal. Let’s see an example:

# Change default palette


sns.set_palette(['#62C370', '#FFD166', '#EF476F'])

# Plot
plt.figure(figsize=(9, 5))
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender')
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1));

Tip #6: sns.set()


From the previous three tips, I hope you will Cnd your favourite
combination (in some cases, it could be leaving the default as is). If we were
to update chart default settings, it’s better to do it just after importing the
visualisation packages. This means we will have a snippet like this at the
beginning of our scripts:

# Import packages
import matplotlib.pyplot as plt
import seaborn as sns

# Change defaults
sns.set_style('whitegrid')
sns.set_context('talk')
sns.set_palette('rainbow')

Updating multiple defaults like above can be done more succinctly with
sns.set() . Here’s the succinct version of the same code:

# Import packages
import matplotlib.pyplot as plt
import seaborn as sns

# Change defaults
sns.set(style='whitegrid', context='talk', palette='rainbow')

Voila These were the six tips. These are the comparison of the plots
before and after tweaking:

I hope you learned a few easy ways to tweak your plots without having to
spend too much time. I hope this post has given you starter ideas to begin
personalising your plots and make them more visually pleasing. If you are
interested, here are links to some of my posts:

Exploratory text analysis in Python


5 tips for pandas users
5 tips for data aggregation in pandas
Writing 5 common SQL queries in pandas
Writing advanced SQL queries in pandas

Bye for now

Sign up for The Daily Pick


By Towards Data Science

Hands-on real-world examples, research, tutorials, and cutting-edge techniques


delivered Monday to Thursday. Make learning your daily ritual. Take a look

Your email Get this newsletter

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.

206 1

Data Visualization Data Science Python Seaborn Matplotlib

More from Towards Data Science Follow

A Medium publication sharing concepts, ideas, and codes.

Giri Rabindranath · Oct 6

BITE-SIZED PERSPECTIVE

Bayes Theorem — It’s as Easy as Checking


the Weather
A simple example to help explain Bayes’ theorem

Photo by NOAA on Unsplash

Bayes’ theorem is a widely used statistical technique. Its applications range


from clinical trials to email classiBcation and its concepts underpin a range of
broader use-cases. But is it possible to understand Bayes’ theorem without
getting bogged down in detailed math or probability theory? Yes, it is — this
article shows you how.

Bayes’ theorem is named after the English statistician and Presbyterian


minister, Thomas Bayes, who formulated the theorem in the mid 1700’s.
Unfortunately, Bayes never lived to see his theorem gain prominence, as it
was published after his death.

Bayes’ theorem has since grown to become a widely used and important
tool in statistics. It underpins a range of applications in science,
engineering, the humanities and artiCcial intelligence. …

Read more · 10 min read

24

Alejandro Saucedo · Oct 6

Real Time Machine Learning at Scale


using SpaCy, Kafka & Seldon Core
A hands on tutorial that covers how to train a machine learning model
using the Reddit comment moderation dataset, and deploy it in a
scalable infrastructure using Kafka and Seldon Core

Model Deployment with Seldon Core for Stream Processing (Image by Author)

In this post, we will cover how to train and deploy a machine learning
model leveraging a scalable stream processing architecture for an
automated text prediction use-case. We will be using Sklearn and SpaCy to
train an ML model from the Reddit Content Moderation dataset, and we
will deploy that model using Seldon Core for real time processing of text
data from Kafka real-time streams. You can also Cnd an overview of the
content in this post in video form, presented at the NLP Summit 2020.
Real Time Machine Learning Video (NLP Summit 2020)

You can Cnd the full code for this article in the following…

Read more · 7 min read

197 1

Leana Critchell · Oct 6

Become an Expert at the Technical


Audition — Part II
This week, I deep dive into the specifics of implementing deliberate
practice in your interview preparation

Photo by Kulik Stepan from Pexels

In Part I of this blog series, I discussed the idea of treating onsite technical
interviews as auditions rather than interviews and that we can utilise
practice techniques adopted by professional musicians to ensure our
preparation is more edcient and geared towards success.

One such practice technique is a method called deliberate practice which I


will deep dive in this blog. Be sure to check out Part III that outlines a study
plan implementing deliberate practice.

Deliberate Practice
In Part I, I touched on the idea of deliberate practice¹, a term coined by Dr
Anders Ericsson and identiCed as a method used by many to excel
themselves to expertise. In this article, I’m going to deep-dive into what
deliberate practice is and suggest how you can apply deliberate practice to
your technical audition preparation. …

Read more · 11 min read

103

Maxime Godfroid · Oct 6

Excel’s Limitation Caused Loss of 16,000


Positive COVID Cases
And 50,000 potentially infectious people missed by tracers and not
told to self-isolate

Photo by Mika Baumeister on Unsplash

Missing Data
If you open an Excel Cle and scroll to the bottom (tip: CTRL + south arrow)
you’ll Cnd out it ends at 1,048,576. Not a single row more.

This limitation was blamed by Public Health England for the loss of 15,841
positive COVID test results, as reported by The Guardian¹. In turn, following
the estimate that each non-complex case has 3 close contacts, it leads to at
least 47,000 potentially infectious people not being informed and required
to self-isolate, and potentially be traced. This Cgure can rise beyond 50,000
as it was reported² that a minority of the 15,841 missing positive cases were
complex, i.e. …

Read more · 5 min read

28 1

Charu Makhijani · Oct 6

Advanced Ensemble Learning Techniques


Ensemble is an art and science

Photo by Jeswin Thomas on Unsplash

In my previous post about ensemble learning, I have explained what is


ensemble learning, how it relates with Bias and Variance in machine
learning and what are the simple techniques of ensemble learning. If you
haven’t read the post, please refer here.

In this post I will cover ensemble learning types, advanced ensemble


learning methods — Bagging, Boosting, Stacking and Blending with code
samples. At the end I will explain some pros and cons of using ensemble
learning.

Ensemble Learning Types


Ensemble learning methods can be categorized into two groups:

1. Sequential Ensemble Methods

In this method base learners are dependent on the results from previous
base learners. Every subsequent base model corrects the prediction made
by its predecessor Cxing the errors in it. Hence the overall performance can
be increased via improving the weight of previous labels. …

Read more · 7 min read

56

Read more from Towards Data Science

More From Medium

How fast is C++ Microservice A Full-Length Machine 12 Data Science


compared to Python? Architecture and its 10 Learning Course in Projects for 12 Days of
Naser Tamimi in Towards
Most Important Design Python for Free Christmas
Data Science
Patterns Rashida Nasrin Sucky in Terence Shin in Towards Data
Md Kamaruzzaman in Towards Data Science Science
Towards Data Science

How We, Two Scheduling All Kinds of How To Create A Fully Noam Chomsky on the
Beginners, Placed in Recurring Jobs with Automated AI Based Future of Deep Learning
Kaggle Competition Top Python Trading System With Andrew Kuo in Towards Data
4% Martin Heinz in Towards Data
Python Science
Andre Ye in Towards Data Science Rubén Romero in Towards
Science Data Science

About Help Legal

You might also like