【问题标题】:How to show multiple data in one plot in python?如何在python中的一个图中显示多个数据?
【发布时间】:2021-07-22 18:55:03
【问题描述】:

我正在使用二进制分类数据集。我想要一个计划,显示第一列显示人们的年龄,下一列显示一年级学生的年龄,第三列显示二年级学生的年龄。请告诉我应该怎么做

age | class
------------
 1 |  1
 2 |  1
 3 |  0
 4 |  1
 5 |  0
 6 |  1
 7 |  1
 8 |  0
 9 |  0
10 |  1



import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv (r'test.csv')

firstClassDf = df[df['class'] == '0']
print(firstClassDf.shape)

print(firstClassDf.head())


secondClassDf = df[df['class'] == '1']
print(secondClassDf.shape)

boxplot1 = df.boxplot(column='age')
plt.figure()

boxplot2 = firstClassDf.boxplot(column='age')
plt.figure()

boxplot3 = secondClassDf.boxplot(column='age')
plt.figure()

print(plt.show())

预期情节

【问题讨论】:

  • 您能预览一下 test.csv 吗? df.head() 就足够了
  • @DSteman 是的,当然
  • 你想要的输出是什么样的?
  • @DSteman 我已经添加了预期的设计
  • 我的数据视图和我上面写的差不多,一栏是人,第二栏是班级。 @paradocslover

标签: python data-science data-mining


【解决方案1】:

从 cmets 我了解到您正在寻找这样的东西:

import pandas as pd
import matplotlib.pyplot as plt

data = [['age', 'class'],
 [1,1],
 [2,1],
 [3,0],
 [4,1],
 [5,0],
 [6,1],
 [7,1],
 [8,0],
 [9,0],
[10,1]]

df = pd.DataFrame(data[1:])
df.columns = data[0]

bar1 = df['age'].mean()
bar2 = df[df['class'] == 0]['age'].mean()
bar3 = df[df['class'] == 1]['age'].mean()
labels = ['all', 'class1', 'class2']

print (plt.bar(labels, [bar1,bar2,bar3]))

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 2012-01-21
    • 2012-05-28
    • 2021-06-10
    • 2021-07-23
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多