6 simple tips for prettier and customised plots in Seaborn (Python) | by Zolzaya Luvsandorj | Towards Data Science

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.
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 packagesimport matplotlib.pyplot as pltimport seaborn as sns# Import datadf = 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:
# Plotsns.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.
You will see that the first two tips are for individual plots whereas the remaining four tips are for changing the default settings for all charts.
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.
# Plotsns.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.
Plots can often benefit from resizing. If we wanted to resize, this is how we would do it:
# Plotplt.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:
# Plotplt.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.
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 stylesns.set_style('whitegrid')# Plotplt.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 find the one you fancy more.
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 different 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 contextsns.set_context('talk')# Plotplt.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.
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 palettesns.set_palette('rainbow')# Plotplt.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 find 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 palettesns.set_palette(['green', 'purple', 'red'])# Plotplt.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 find a custom colour palette in hexadecimal. Let’s see an example:
# Change default palettesns.set_palette(['#62C370', '#FFD166', '#EF476F'])# Plotplt.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));
From the previous three tips, I hope you will find 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 packagesimport matplotlib.pyplot as pltimport seaborn as sns# Change defaultssns.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 packagesimport matplotlib.pyplot as pltimport seaborn as sns# Change defaultssns.set(style='whitegrid', context='talk', palette='rainbow')
Voila❕ These were the six tips. These are the comparison of the plots before and after tweaking:
Would you like to access more content like this? Medium members get unlimited access to any articles on Medium. If you become a member using my referral link, a portion of your membership fee will directly go to support me.
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 🏃💨