Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Distance betweeen groups in a matplotlib bar plot

Here is how I bar-plot from a group:

import numpy as np
import matplotlib.pyplot as plt

metrics = ['accuracy', 'precision', 'recall','f1_score', 'roc_auc_score']
x_values = np.arange(len(metrics))
width = 0.15

RF = [0.62, 0.59, 0.62, 0.57, 0.78]
SMOTE = [0.63, 0.62, 0.63, 0.60, 0.79]
AdaBoost = [0.27, 0.42, 0.27, 0.28, 0.58]
SMOTEBoost = [0.54, 0.60, 0.54, 0.57, 0.68]
decoc = [0.63, 0.61, 0.63, 0.58, 0.69]

plt.bar(x_values-0.2, RF, width=width, label='RF')
plt.bar(x_values, SMOTE, width=width, label='SMOTE')
plt.bar(x_values+0.2, AdaBoost, width=width, label='AdaBoost')
plt.bar(x_values+0.4, SMOTEBoost, width=width, label='SMOTEBoost')
plt.bar(x_values+0.6, decoc, width=width, label='DECOC')
plt.xticks(x_values, metrics)

plt.legend(loc='best')

plt.ylim(0.0, 1.2)

plt.title('Performance Evaluation')
plt.xlabel('Performance Metrics')
plt.show()

Figure:
enter image description here

But I need a space between each group ('accuracy', 'precision', 'recall','f1_score', 'roc_auc_score') to make it better. As it is, groups are mixed with almost no space separating.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Any reason to do multiple bar plots instead of one and pass the hue variable?

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (16,8)

RF = [0.62, 0.59, 0.62, 0.57, 0.78]
SMOTE = [0.63, 0.62, 0.63, 0.60, 0.79]
AdaBoost = [0.27, 0.42, 0.27, 0.28, 0.58]
SMOTEBoost = [0.54, 0.60, 0.54, 0.57, 0.68]
decoc = [0.63, 0.61, 0.63, 0.58, 0.69]
metrics = ['accuracy', 'precision', 'recall','f1_score', 'roc_auc_score']

df = pd.DataFrame({"metrics":metrics,"RF":RF, "SMOTE":SMOTE,"AdaBoost":AdaBoost,"SMOTEBoost":SMOTEBoost,"decoc":decoc})
df = pd.melt(df, id_vars="metrics")

sns.barplot(data=df, x="metrics", y="value", hue="variable")
plt.legend(loc='best')

plt.ylim(0.0, 1.2)

plt.title('Performance Evaluation')
plt.xlabel('Performance Metrics')
plt.show()

seaborn bar plot

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading