A beginner’s guide to data visualization with Python and Seaborn
What is Seaborn?
Seabornis a library for making statistical graphics in Python. It builds on top ofmatplotliband integrates closely withpandas data structures.
Seaborn design allows you to explore and understand your data quickly. Seaborn works by capturing entire data frames or arrays containing all your data and performing all the internal functions necessary for semantic mapping and statistical aggregation to convert data into informative plots.
It abstracts complexity while allowing you to design your plots to your requirements.
[Read:Meet the 4 scale-ups using data to save the planet]
Installing Seaborn
Installingseabornis as easy as installing one library using your favorite Python package manager. When installingseaborn, the library will install its dependencies, includingmatplotlib,pandas,numpy, andscipy.
Let’s then install Seaborn, and of course, also the packagenotebookto get access to our data playground.
Additionally, we are going to import a few modules before we get started.
Building your first plots
Before we can start plotting anything, we need data. The beauty ofseabornis that it works directly withpandasdataframes, making it super convenient. Even more so, the library comes with some built-in datasets that you can now load from code, no need to manually downloading files.
Let’s see how that works by loading a dataset that contains information about flights.
Extending with matplotlib
Seaborn builds on top ofmatplotlib, extending its functionality and abstracting complexity. With that said, it does not limit its capabilities. Anyseabornchartcan be customized using functions from thematplotliblibrary. It can come in handy for specific operations and allows seaborn to leverage the power ofmatplotlibwithout having to rewrite all its functions.
Let’s say that you, for example, want to plot multiple graphs simultaneously usingseaborn; then you could use thesubplotfunction frommatplotlib.
Using thesubplotfunction, we can draw more than one chart on a single plot. The function takes three parameters, the first is the number of rows, the second is the number of columns, and the last one is the plot number.
We are rendering aseabornchartin each subplot, mixingmatplotlibwithseabornfunctions.
Seaborn loves Pandas
We already talked about this, butseabornlovespandasto such an extent that all its functions build on top of thepandasdataframe. So far, we saw examples of usingseabornwith pre-loaded data, but what if we want to draw a plot from data we already have loaded usingpandas?
Making beautiful plots with styles
Seaborn gives you the ability to change your graphs’ interface, and it provides five different styles out of the box:darkgrid,whitegrid,dark,white, andticks.
Here is another example
Cool use cases
We know the basics ofseaborn, now let’s get them into practice by building multiple charts over the same dataset. In our case, we will use the dataset “tips” that you can download directly usingseaborn.
First, load the dataset.
Let’s create an additional column to the data set with the percentage that represents the tip amount over the total of the bill.
Next, we can start plotting some charts.
Conclusion
Of course, there’s much more we can do withseaborn, and you can learn more use cases by visiting theofficial documentation. I hope that you enjoyed this article as much as I enjoyed writing it.
Thisarticlewas originally published onLive Code StreambyJuan Cruz Martinez(twitter:@bajcmartinez), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.
Live Code Streamis also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.