Use this One Line of Code to Create Beautiful Data Visualizations in Python

data visualization Oct 04, 2022
 

In this post, you'll learn the absolute quickest path to create beautiful data visualizations in Python. Specifically, we will be issuing a command from Seaborn, a popular library for creating static two-dimensional plots. We begin by reading in some data from Airbnb listings in Washington D.C.

In [1]:
import pandas as pd
df = pd.read_csv('../../data/airbnb.csv', 
                 usecols=['neighborhood', 'accommodates', 'price'])
df.head()
Out[1]:
  neighborhood accommodates price
0 Shaw 3 90
1 Union Station 2 95
2 Dupont Circle 1 90
3 Dupont Circle 2 195
4 Columbia Heights 3 99
 

Create the default box plot

Here, we will use Seaborn to create a box plot of prices by neighborhood splitting by the number of persons the space accommodates.

In [2]:
import seaborn as sns
sns.boxplot(x='neighborhood', y='price', hue='accommodates', data=df);
 
 

The set_theme function

While the default plot settings are not bad, they can be improved upon using a single command and that is with the set_theme function in Seaborn. By default, it will use the darkgrid style along with the deep color palette.

In [3]:
sns.set_theme()
 

After running this command, the same plot will appear substantially different.

In [4]:
sns.boxplot(x='neighborhood', y='price', hue='accommodates', data=df);
 
 

Customizing matplotlib run configuration settings

You may call the set_theme function without arguments, but I suggest increasing the DPI (dots per square inch) and reducing the font scale. The default DPI is 100 for matplotlib (the library Seaborn uses for all of its plotting), which is a lower resolution than most modern monitors and is the reason why the default image size (6.4 inches by 4.8 inches) does not match the actual screen inches on your monitor.

Any plot settings (formally, the matplotlib run configuration parameters) are able to changed with the rc parameter. Here, the DPI is increased to 150 and the default figure size decreased.

In [5]:
sns.set_theme(rc={'figure.dpi': 150, 'figure.figsize': (5, 3.75)})
sns.boxplot(x='neighborhood', y='price', hue='accommodates', data=df);
 
 

Scaling the font

While the image is sharper and larger, the text is clearly too big. The set_theme function provides a font_scale parameter to decrease the relative size of the text in the image. Here we set it to 0.65.

In [6]:
sns.set_theme(rc={'figure.dpi': 150, 'figure.figsize': (5, 3.75)}, font_scale=0.65)
sns.boxplot(x='neighborhood', y='price', hue='accommodates', data=df);
 
 

Choosing other styles and color palettes

There are several built-in styles (white, dark, whitegrid, darkgrid, ticks) as well as color palettes (deep, muted, bright, pastel, dark, colorblind) available as well. Here, we choose the whitegrid style paired with the pastel color palette.

In [7]:
sns.set_theme(style='whitegrid', 
              palette='pastel', 
              rc={'figure.dpi': 150, 'figure.figsize': (5, 3.75)}, 
              font_scale=0.65)
sns.boxplot(x='neighborhood', y='price', hue='accommodates', data=df);
 
 

Summary

In summary, use the set_theme function in Seaborn to easily choose a style, color palette, customize the matplotlib run configuration settings and to scale the font.

Close

Register for a free account

Upon registration, you'll get access to the following free courses:

  • Python Installation
  • Intro to Jupyter Notebooks
  • Intro to Pandas
  • Python  and Pandas Challenges