python-3.x - + 的错误不支持的操作数类型:“int”和“method”
问题描述
我的条形图有什么问题?
当我在这个 y 轴上使用变量名称时,条形图不显示
y = [totalMales, totalFemales] # if I input numerical values it works, but it doesn't with variable names
#check total number of males
totalMales = newDF.loc[(newDF.Gender=='Male')].count #409
print("totalMales" + str(totalMales))
totalFemales = newDF.loc[(newDF.Gender=='Female')].count() #77
print("totalFemales" + str(totalFemales))
#males are more likely to borrow than females 409 > 77
plt.style.use('ggplot')
x = ['Males', 'Females']
y = [totalMales, totalFemales] # if I input numerical values it works, but it doesn't with variable names
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, y, color='green')
plt.xlabel("Gender")
plt.ylabel("Total who Paid Off")
plt.title("Number of Males vs Females who Paid Off")
plt.xticks(x_pos, x)
plt.show()
解决方案
当您拥有 DataFrame 时,可以使用更多内置函数。随着value_counts
某一列中的不同值被计算并作为一个系列返回。使用.plot.bar()
它可以直接在条形图中绘制。x-as 上的标签直接代表不同的性别。
# Count the values
gender_count = newDF.Gender.value_counts()
# Create plot
gender_count.plot.bar()
# Settings for plot
plt.style.use('ggplot')
plt.xlabel("Gender")
plt.ylabel("Total who Paid Off")
plt.title("Number of Males vs Females who Paid Off")
plt.show()
推荐阅读
- android - 切换按钮总是显示 false state_checked 图标
- android - 从 Android 中的挂起函数并行调用 Kotlin 协程
- smtp - 每个收件人的 SMTP + E2E 加密
- amazon-web-services - 从 terraform 的现有 ec2 实例创建 Ec2
- timer - 输出比较匹配不发生 - AVR Atmega32
- javascript - react-router-dom 中的自定义提示组件。防止页面重新加载
- bash - “bash: command not found”使用while循环在bash中执行算术运算
- javascript - Angular 反应形式中的选择和选项标签
- r - 如果只有几个字符匹配,我可以匹配和替换数据吗?
- html - 使 SVG 文本元素绝对定位(即不占用空间)?