2. Using seaborn for smoother data visualisation#

The seaborn package is built on top of Matplotlib and provides a much easier way of interacting with data. In fact, its built to work with Pandas DataFrames in mind, and is capable of creating complete plots in a single line of code, as well as introducing a multitude of different plot types that Matplotlib can’t create out of the box.

But because seaborn is built from Matplotlib, you still have full control of the different plotting elements that you had before.

The module is imported with its traditional alias, sns.

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

# Show a simple plot with the tips data
# Note different function name to distinguish between matplotlib
tips = sns.load_dataset('tips')
graph = sns.scatterplot(data=tips, x='tip', y='total_bill', hue='sex', alpha=0.5)
../_images/seaborn_1_0.png

Modifying the aspects of our figure is straightforward - by accessing the relevant methods and attributes. To see it again, we can use the .figure attribute of the variable.

# Change axis labels, and legend location
graph.set_ylabel('Total Bill Amount')
graph.set_xlabel('Tip Amount')
graph.legend(loc='upper right')

# Show
graph.figure
../_images/seaborn_3_0.png