Chapter 36. Visualization with Seaborn
Matplotlib has been at the core of scientific visualization in Python for decades, but even avid users will admit it often leaves much to be desired. There are several complaints about Matplotlib that often come up:
-
A common early complaint, which is now outdated: prior to version 2.0, Matplotlib’s color and style defaults were at times poor and looked dated.
-
Matplotlib’s API is relatively low-level. Doing sophisticated statistical visualization is possible, but often requires a lot of boilerplate code.
-
Matplotlib predated Pandas by more than a decade, and thus is not designed for use with Pandas
DataFrameobjects. In order to visualize data from aDataFrame, you must extract eachSeriesand often concatenate them together into the right format. It would be nicer to have a plotting library that can intelligently use theDataFramelabels in a plot.
An answer to these problems is Seaborn. Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas.
To be fair, the Matplotlib team has adapted to the changing landscape:
it added the plt.style tools discussed in
Chapter 34, and Matplotlib is starting to handle
Pandas data more seamlessly. But for all the reasons just discussed,
Seaborn remains a useful add-on.
By convention, Seaborn is often imported as sns:
In[1