社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何用python绘制条形图?

Alpenliee • 3 年前 • 1230 次点击  

我想可视化一个带有两个类的条形图。Class=0表示蓝色,Class=1表示红色。

#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() 

错误代码: ValueError:形状不匹配:无法将对象广播到单个形状

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132712
 
1230 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Ishan Shishodiya
Reply   •   1 楼
Ishan Shishodiya    3 年前

而不是使用 matplotlib 的条形图要绘制每个类的计数,您可以 seaborn 是的 countplot .

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

输出-

enter image description here

chongkai Lu
Reply   •   2 楼
chongkai Lu    3 年前
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

你的错误可能来自错误的类型 real fake .顺便说一句,对于为条形图配置不同的颜色,有一个更好的方法 solution .