The author | Zolzaya Luvsandorj compile | source of vitamin k | forward Datas of Science

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

Baseline figure

The script in this article was tested in python3.8.3 in the Jupyter notebook.

Let’s use Seaborn’s built-in Penguins dataset as 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
Copy the code

We will build a standard scatter chart using the default chart Settings 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')
Copy the code

We will see how this diagram changes with each technique.


skills

As you will see, the first two tips are used for a single drawing, while the remaining four tips are used to change the default Settings for all charts.

Tip 1: semicolons

Did you notice in the previous figure that the text output was right at the top of the chart? An easy 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');
Copy the code

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

Tip 2: plt.figure()

Drawing can 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');
Copy the code

When we resize, the legend moves to the upper left corner. Let’s move the legend out of the diagram so 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));
Copy the code

If you want to know how to figure out what combination of numbers figsize() or bbox_to_anchor() uses, you need to try out which numbers are best for drawing.

Tip 3: sns.set_style()

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

Change the 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));
Copy the code

Here are a few other options to try: “Darkgrid,” “Dark” and “Ticks” to find the one you like better.

Tip 4: sns.set_context()

In the previous figure, the label size looks small. If you don’t like the default setting, you can change context parameters using sns.set_context().

I use this function mainly to control the default font size for labels in my drawing. By changing the default values, we can save time by not having to adjust font sizes for different elements of a single drawing (for example, axis labels, headings, legends). Let’s change the context to “talk” and look at the picture:

# 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));
Copy the code

It’s easier to recognize, isn’t it? Another option to try is “Poster”, which increases the default size even more.

Tip 5: sns.set_palette()

This feature is handy if you want to customize the default palette to your preferred color combination. We can use color mapping in Matplotlib. This is a selection from the color library. Let’s change the palette to “Rainbow” and look at the image again:

Change the 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));
Copy the code

If you can’t find a Matplotlib color map you like, 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 a function, as shown in the following example. This link is the color name list: matplotlib.org/3.1.0/galle…

Change the 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));
Copy the code

If color names don’t quite capture what you’re after, you can use hexadecimal colors to build your own palette to access a wider range of options (over 16 million colors!). . Here is my favorite resource to find a custom color palette in hexadecimal. Let’s look at an example:

Change the 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));
Copy the code

Tip 6: sns.set()

From the previous three tips, I hope you find your favorite combination (in some cases, it may leave the default Settings). If we are going to update the default Settings of the diagram, it is best to do so after importing the visualization package. This means we have a snippet at the beginning of the script like this:

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

Change the default value
sns.set_style('whitegrid')
sns.set_context('talk')
sns.set_palette('rainbow')
Copy the code

Multiple defaults above can be updated with sns.set(). Here’s a concise version of the same code:

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

Change the default value
sns.set(style='whitegrid', context='talk', palette='rainbow')
Copy the code

Here are six tips. Here’s the comparison before and after:


I hope you’ve learned some simple ways to tweak your charts that don’t take too much time. I hope this article gives you some initial ideas to start personalizing your charts and making them more visually beautiful. If you are interested, here are links to some of my posts:

  • Towardsdatascience.com/exploratory…

  • Towardsdatascience.com/5-tips-for-…

  • Towardsdatascience.com/writing-5-c…

  • Towardsdatascience.com/writing-5-c…

  • Towardsdatascience.com/writing-adv…

Original link: towardsdatascience.com/6-simple-ti…

Welcome to panchuangai blog: panchuang.net/

Sklearn123.com/

Welcome to docs.panchuang.net/