Seaborn’s six simple tips

Time:2021-8-7

Author | zolzaya luvsandorj
Compile VK
Source: towards Data Science

In this article, we will explore some simple ways to customize your charts to make them aesthetically better. I hope these simple techniques can help you get a better picture.

Baseline map

The script in this article was tested in Python 3.8.3 in the Jupiter notebook.

Let’s use the built-in penguins dataset of Seaborn as the sample data:

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

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

We will use the default chart settings to build a standard scatter chart to use as a baseline:

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

We’ll see how this diagram changes with each technique.


skill

As you will see, the first two techniques are used for a single plot, while the remaining four techniques are used to change the default settings for all charts.

Tip 1: semicolon

Have you noticed that in the previous diagram, the text output is right above the diagram? A simple way to suppress this text output is to use it at the end of the drawing;

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

Just add at the end of the code; You can get clearer output.

Tip 2: PLT. Figure ()

Drawings often benefit from resizing. If we want to resize, we can do this:

#Figure
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 resize, the legend moves to the upper left corner. Let’s move the legend out of the chart so that it doesn’t accidentally overwrite data points:

#Figure
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 want to know how to knowfigsize()orbbox_to_anchor()What number word combinations are used, you need to try which numbers are best for drawing.

Tip 3: sns.set_ style()

If you don’t like the default style, this function helps to change the overall style of the drawing. This includes the color and background of the axis. Let’s change the style to whitegrid and see how the print appearance changes:

#Change default style
sns.set_style('whitegrid')

#Figure
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 other options to try: “darkgrid”, “dark” and “ticks” to find the one you prefer.

Tip 4: sns.set_ context()

In the previous figure, the label size looks small. If you don’t like the default settings, we use sns.set_ Context() can change context parameters.

I use this function mainly to control the default font size of labels in the drawing. By changing the default values, we can save time without having to adjust the font size for different elements of a single drawing, such as axis labels, titles, legends. Let’s change the context to “talk” and look at the figure:

#Default context change
sns.set_context('talk')

#Figure
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 easier to identify, isn’t it? Another option you can try is “poster”, which will increase the default size or more.

Tip 5: sns.set_ palette()

This feature is very convenient if you want to customize the default palette to your favorite color combinations. We can use color mapping in Matplotlib. Here is a selection from the color library. Let’s change the palette to “Rainbow” and look at the figure again:

#Change default palette
sns.set_palette('rainbow')

#Figure
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 find your favorite Matplotlib color map, you can manually select colors to create your own unique palette. One way to create your own palette is to pass a list of color names to the function, as shown in the following example. This link is a list of color names:https://matplotlib.org/3.1.0/gallery/color/named_colors.html。

#Change default palette
sns.set_palette(['green', 'purple', 'red'])

#Figure
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 color names don’t capture what you’re looking for, you can use hexadecimal colors to build your own palette to access a wider range of options (more than 16 million colors!). Here is my favorite resource. You can find a hexadecimal custom palette. Let’s take an example:

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

#Figure
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 can find your favorite combination (in some cases, it may keep the default settings). If we want to update the default settings of the chart, it is best to update it after importing the visualization package. This means that we will have such a fragment at the beginning of the script:

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

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

SNS. Set() can be used to update multiple default values above. The following is a concise version of the same code:

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

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

Here are six tips. The following is the comparison diagram before and after adjustment:


I hope you’ve learned some simple ways to adjust your chart. It doesn’t take much time. I hope this article can give you some preliminary ideas to personalize your charts and make them more visually beautiful. If you are interested, here are some links to my posts:

Original link:https://towardsdatascience.com/6-simple-tips-for-prettier-and-customised-plots-in-seaborn-python-22f02ecc2393

Welcome to panchuang AI blog:
http://panchuang.net/

Official Chinese document of sklearn machine learning:
http://sklearn123.com/

Welcome to panchuang blog resources summary station:
http://docs.panchuang.net/