如何使用for循环绘制直方图和条形图并一起显示?

时间:2019-10-08 12:08:27

标签: python charts histogram

我想为数字数据绘制直方图,为分类数据绘制条形图。我已经对已经存储在types中的数据类型进行了分类。

我不确定如何将plt.hist()plt.bar()放入我的for循环中,以便并排显示2个直方图(一个用于年龄,另一个用于保存)。类别数据也一样,我想要2个类别数据(性别和工作)的2条形图。该代码还能够计算数据的频率,以便绘制直方图。

这意味着最后,我将有4个图表,2个直方图和2个条形图并排显示。

import numpy as np
from scipy import stats

# first data is age
# 2nd data is gender
# third data is saving
# 4th data is job

data = np.array([[11, "male",1222,"teacher"],[23,"female",333,"student"],
                 [15,"male",542,"security"],[23,"male",4422,"farmer"],[25,"female",553,"farmer"],
                 [22, "male", 221, "teacher"],[27, "male", 333, "agent"],[11, "female", 33, "farmer"]])

# type of the data above
types = ["num","cat","num","cat"]
idx1 = []
idx2 = []
for index, _type in enumerate(types):
    if _type == 'num':
        idx1.append(index)
    else:
        idx2.append(index)

mean = []
std = []
for i in idx1:
    mean.append(np.mean(data[:,i].astype("float32")))
    std.append(np.std(data[:,i].astype("float32")))

print("mean: {}".format(mean))
print("std: {}".format(std))

mode = []
for i in idx2:
    mode.append(stats.mode(data[:,i]))
print("mode: {}".format([sub[0][0] for sub in mode]))

1 个答案:

答案 0 :(得分:0)

我将让您弄清楚该图的详细信息以及如何自定义它们,但我的建议是改为将其变成DataFrame对象。

data = pd.DataFrame({'Age': [11, 23, 15, 23, 25, 22, 27,11],
                     'Gender': ['M', 'F', 'M', 'M', 'F', 'M', 'M', 'F'],
                     'Saving': [1222, 333, 542, 4422, 553, 221, 333, 333],
                     'Occupation': ['teacher', 'student', 'security', 'farmer', 'farmer', 'teacher', 'agent', 'farmer']})

fig, ax = plt.subplots(nrows = 2, ncols = 2, figsize=(10, 5))

# Plot Data
plt.subplot(1,2,1)
plt.hist(data['Age'], edgecolor = 'k')
plt.subplot(1,2,2)
plt.hist(data['Saving'], edgecolor = 'k')
plt.show()
plt.subplot(2, 1, 1)
data['Gender'].value_counts().plot(kind = 'bar')
plt.show()
plt.subplot(2, 2, 2)
data['Occupation'].value_counts().plot(kind = 'bar')
plt.show()