Home > Mobile >  Distance betweeen groups in a matplotlib bar plot
Distance betweeen groups in a matplotlib bar plot

Time:07-14

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.

CodePudding user response:

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

CodePudding user response:

Increase the spacing between the x-values:

x_values = np.arange(0, len(metrics)*2, 2)
  • Related