Created
January 23, 2023 15:49
-
-
Save muhammadanas0716/30b49dca24dab47199f961fc666baffe to your computer and use it in GitHub Desktop.
The diverging bars are a good way to see how different things change based on one measure. It helps you see the difference in the performance of groups in your data and is easy to understand and shows the point clearly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the needed libs | |
import numpy as np | |
import pandas as pd | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
# Prepare Data | |
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv") | |
x = df.loc[:, ['mpg']] | |
df['mpg_z'] = (x - x.mean())/x.std() | |
df['colors'] = ['red' if x < 0 else 'green' for x in df['mpg_z']] | |
df.sort_values('mpg_z', inplace=True) | |
df.reset_index(inplace=True) | |
# Draw plot | |
plt.figure(figsize=(14,10), dpi= 80) | |
plt.hlines(y=df.index, xmin=0, xmax=df.mpg_z, color=df.colors, alpha=0.4, linewidth=5) | |
# Decorations | |
plt.gca().set(ylabel='$Model$', xlabel='$Mileage$') | |
plt.yticks(df.index, df.cars, fontsize=12) | |
plt.title('Diverging Bars of Car Mileage', fontdict={'size':20}) | |
plt.grid(linestyle='--', alpha=0.5) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment