Plotting Data with Seaborn
This is a continuation of my “Data Visualization with Python” post. In this post, I have decided to cover plotting in the Python library seaborn which is an alternative to
Plotting with Seaborn
As I mentioned earlier, seaborn is an alternative data visualization library based on matplotlib but has a different api and different methods to plot data.
Column Chart
You can plot data into a column chart using the below code snippet:
import seaborn as sns
import pickle
# Load data which is stored as binary
with open ('fruit-sales.pickle', 'rb') as f:
data = pickle.load(f)
# Splitting a list of tuples into two lists
fruit, num_sold = zip(*data) # The * unpacks the iterable
# Convert into lists as seaborn can't read tuples for all functions
fruit = list(fruit)
num_sold = list(num_sold)
'''
Plotting the data in a column chart
'''
axes = sns.barplot(x=fruit, y=num_sold)
# Chart annotations
axes.set_title('Number of Fruits Sold (2017)')
axes.set_ylabel('Number of Fruit (millions)')
This code results in the below chart:
Bar Chart
You can plot data into a Bar chart using the below code snippet:
import seaborn as sns
import pickle
# Load Data
with open('coding-exp-by-dev-type.pickle', 'rb') as f:
data = pickle.load(f)
# Split into two lists
dev_types, years_exp = zip(*data)
dev_types = list(dev_types)
years_exp = list(years_exp)
'''
Plotting the data in a bar chart
'''
# Switching the x/y order to y/x makes the bar chart horizontal
axes = sns.barplot(y=dev_types, x=years_exp)
# Chart Annotations
axes.set_title('Years of Coding Experience by Developer Type')
axes.set_xlabel('Years')
This code results in the below chart:
Update
January 24, 2021
I wanted to post an update on this post as it has been a while since I started it, and I never finished the different kind of plots you can do in Seaborn. At this time, I will not be updating this post any further as I do not know if I will be using Seaborn to plot in the future. There are so many different tools, some that I am more eager to use than Seaborn. I also have a project coming up were I will be scraping, analyzing, and plotting data, so look out for that one if you are interested in this kind of stuff.
Thanks for reading!