0

I want to visualize a bar plot with 2 classes. Class=0 its color blue and class=1 its color red.

#here its my code
x = ['0','1']
real = df[df['fake'] == 0].count()
fake = df[df['fake'] == 1].count()
plt.bar(x, real, color='blue')
plt.bar(x, fake, color='red')
plt.title("Class Fake & Real")
plt.show() 

Error code : ValueError: shape mismatch: objects cannot be broadcast to a single shape

Alpenliee
  • 36
  • 4

2 Answers2

0
from matplotlib import pyplot as plt
x = ['0', '1']
real, fake = 5, 10
plt.bar(x[0], real, color='blue')
plt.bar(x[1], fake, color='red')
plt.title("Class Fake & Real")
plt.show()

enter image description here

Your error may comes from wrong types of real and fake. BTW, for configuring different colors for bars, there is a better solution.

chongkai Lu
  • 452
  • 3
  • 9
0

Instead of using matplotlib's bar plot to plot the counts for each class, you can you seaborn's countplot.

plt.figure(figsize = (7,5))
sns.countplot(x = "fake", data = df, palette=["blue","red"])
plt.show() 

Output -

enter image description here

Zero
  • 1,800
  • 1
  • 5
  • 16