6 Simple Tips For Prettier and Customised Plots in Seaborn (Python) - by Zolzaya Luvsandorj - Toward
6 Simple Tips For Prettier and Customised Plots in Seaborn (Python) - by Zolzaya Luvsandorj - Toward
6 Simple Tips For Prettier and Customised Plots in Seaborn (Python) - by Zolzaya Luvsandorj - Toward
◼ ❕ 🌟 🏃💨 📊
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
(Python)
Beginner’s guide to easily personalise your plots
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.
Baseline plot
The scripts in this post are tested in Python 3.8.3 in Jupyter Notebook.
# 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.
# Plot
sns.scatterplot(data=df, x='body_mass_g', y='bill_length_mm',
alpha=0.7, hue='species', size='gender');
# 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
# 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.
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:
# 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.
# 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.
# 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:
# 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));
# 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:
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
BITE-SIZED PERSPECTIVE
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. …
24
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…
197 1
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.
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. …
103
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. …
28 1
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. …
56
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