0
$\begingroup$

So for some weird reason I can't manage to fix the plotting issue

Any suggestions?

enter image description here

from sklearn.metrics import confusion_matrix

List = [] 
for i in range(len(model)):
  cm = confusion_matrix(Y_test, model[i].predict(X_test))

  TN = cm[0][0]
  TP = cm[1][1]
  FN = cm[1][0]
  FP = cm[0][1]

  List.append(((TP + TN) / (TP + TN + FN + FP))*100)

  print()

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
bars = ('Logistic', 'K Nearest Neighbor', 'K Nearest Neighbor', 'Support Vector Machine ', 'Support Vector Machine ','Support Vector Machine ','Support Vector Machine ' )
percentage = np.array([List[0], List[1], List[2], List[3],List[4],List[5],List[6]])


new_labels = [i+'  {:.2f}%'.format(j) for i, j in zip(bars, percentage)]

plt.barh(bars, percentage, color='lightskyblue', edgecolor='blue')
plt.yticks(range(len(bars)), new_labels)
# Show graphic
plt.show()
$\endgroup$
2
  • $\begingroup$ Hello @Omerlewitz, welcome to the site. Your answer seems quite vague, have a look here to see how to ask better questions. Nevertheless, could you describe better what your desired outcome would be? $\endgroup$ Commented Apr 17, 2020 at 18:14
  • $\begingroup$ each algorithm will be filled to the percentage it should be and the bar will be blue Like the bottom ones $\endgroup$
    – Omerlewitz
    Commented Apr 17, 2020 at 18:23

1 Answer 1

1
$\begingroup$

You have to change one line in your code.

This

plt.barh(bars, percentage, color='lightskyblue', edgecolor='blue')

should become this

plt.barh(range(len(bars)), percentage, color='lightskyblue', edgecolor='blue')

What you are changing is the y position of your bars. If you look at your original bars there are only 3 values there, so bars were overlapping.

Once you change that to range(len(bars)) you are using the same tick values that you set in plt.yticks(range(len(bars)), new_labels).

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.