This post extends the post on bar graph in matplotlib. Bar graph/chart is a common type of visualization used to present categorical data using rectangular bars. Bar charts come in different types such as vertical, horizontal, stacked (either vertical or horizontal), grouped and 100% stacked bar charts. It is used to compare the relative sizes between two or more categories of data. In this post we will look at Bar Graphs and how to create them in seaborn.

seaborn-logo

When to Use Bar Graph

  1. Compare between two or more different groups in data.
  2. Track change over time. Note: Suitable when the changes are too large otherwise use line graph.

How to Use Bar Graph

  1. Bars should be of same type. Avoid mixing 3D and 2-D bars.
  2. The scale of values should always start from zero unless otherwise.
  3. Select the colours to use carefully.
  4. Order the bars with some criteria that best presents the insights.
  5. To maximise the visibility and appearance of all components use different variations of bar charts (vertical, horizontal, grouped etc.).
  6. Include values in the bars for users to make actual comparison between groups.

Bar Graph in Seaborn

Load Data

                    

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_theme(style="whitegrid")
sns.set(rc={'figure.figsize':(20,10)}) # Set figure size 
sns.set(font_scale = 2)

score_df = pd.DataFrame(
    {
        "Students": ["Tom", "Peter","Simon", "Mary", "Jane","King","Hillary","Ethan","Page"],
        "Math": [79.00, 67.00,80.00, 84.00, 70.00,60.00,90.00,76.00,75],
        "Physics":[63.00, 98, 60.00, 90,84.00, 77.00,55.00,70,66.00],
        "Computer":[84.00,78.00, 57.00, 88.00, 75.00,93.00,92.00,98.00,90.00],
    },
    index=["Tom", "Peter","Simon", "Mary", "Jane","King","Hillary","Ethan","Page"]
)

score_df['Total']=score_df[['Math','Physics','Computer']].apply(np.sum,axis=1)
score_df

matplotlib-data

Simple Bar Chart

                    

plt.rcParams['axes.labelsize'] = 20
sns.set(font_scale = 2)
sns.barplot(x=score_df['Students'], y=score_df['Total'])
plt.xticks(rotation=45)
plt.show()

seaborn-simple-bar-chart

Horizontal Bar Chart

                    

plt.rcParams['axes.labelsize'] = 20
sns.set(font_scale = 2)
sns.barplot(y=score_df['Students'], x=score_df['Total'])
plt.xticks(rotation=45)
plt.show()

seaborn-simple-horizontal-bar-chart

Grouped Bar Chart

                    

score_melt_df=pd.melt(score_df.reset_index().head(),id_vars=['Students'],value_vars=['Math','Physics','Computer'],
        value_name='Score')
plt.rcParams['axes.labelsize'] = 20
sns.set(font_scale = 2)
sns.catplot(data=score_melt_df, kind="bar", x="Students", y="Score", hue="variable",height=15, aspect=1.5)
plt.xticks(rotation=45)
plt.show()

seaborn-grouped-vertical-bar-chart

Stacked Bar Chart

                    

plt.rcParams['axes.labelsize'] = 20
sns.set(font_scale = 2)
score_df[['Students','Math','Physics','Computer']].set_index('Students').plot(kind='bar', stacked=True)
plt.xticks(rotation=45)
plt.show()

seaborn-stacked-vertical-bar-chart

For complete code check the jupyter notebook here.

Conclusion

Bar charts are important type of visualizations for presenting data with discrete groups. They are used to compare relative relationship between categories with bars. They are also useful in showing trend between large time intervals. In this post we have looked at what’s bar chart, when to use them and how to use them with seaborn. In the next post we will look at Pie Charts and how to use them in seaborn. To learn about line graph in seaborn check our previous post here. You can also learn about bar chart in matplotlib in our other post here.

Bar Graph in Seaborn

Post navigation


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x