如何在 python 中的一个图中显示多个数据?
How to show multiple data in one plot in python?
我正在使用二进制分类数据集。我想要一个计划,其中第一列显示人们的年龄,下一列显示一年级学生的年龄,第三列显示二年级学生的年龄。请指教我该怎么做
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())
预期剧情
根据评论我了解到您正在寻找这样的东西:
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]))
我正在使用二进制分类数据集。我想要一个计划,其中第一列显示人们的年龄,下一列显示一年级学生的年龄,第三列显示二年级学生的年龄。请指教我该怎么做
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())
预期剧情
根据评论我了解到您正在寻找这样的东西:
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]))