# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
orig_data = [81, 73, 77, 79, 80, 78, 85, 80, 68, 90, 80, 89, 82, 81, 84, 72, 83, 77, 79, 75]
range_data = [] #组中值
x_label = []
polar_diff = []
for i in range(5):
min1 = 67.5 + i * 5
max1 = min1 + 5
x_label.append(str(min1) + "~" + str(max1))
group = [j for j in orig_data if ((j > min1) and (j <= max1))]
print("group", i, ":", group)
range_data.append(len(group))
polar_diff.append(max(group) - min(group))
print("range_data:", range_data)
print("polar_diff:", polar_diff)
bar_wid, inter_space = 0.5, 0.1
group_x = [(k * (bar_wid * 2 + inter_space)) for k in range(len(x_label))]
polar_diff_x = [(k + bar_wid) for k in group_x]
xlabel_x = [(k + bar_wid / 2) for k in group_x]
print("group_x:", group_x)
print("polar_diff_x:", polar_diff_x)
print("xlabel_x:", xlabel_x)
plt.rcParams["font.family"] = "STSong"
plt.bar(group_x, range_data, label = "组中值", width = bar_wid)
plt.bar(polar_diff_x, polar_diff, label = "极差", width = bar_wid)
plt.xticks(xlabel_x, x_label)
plt.title("xxx直方分布图", fontsize = 24)
plt.xlabel("组别", fontsize = 14)
plt.ylabel("组中值(个数)/极差(跳动次数)", fontsize = 14)
plt.legend()
plt.show()
[log输出]
group 0 : [68, 72]
group 1 : [73, 77, 77, 75]
group 2 : [81, 79, 80, 78, 80, 80, 82, 81, 79]
group 3 : [85, 84, 83]
group 4 : [90, 89]
range_data: [2, 4, 9, 3, 2]
polar_diff: [4, 4, 4, 2, 1]
group_x: [0.0, 1.1, 2.2, 3.3000000000000003, 4.4]
polar_diff_x: [0.5, 1.6, 2.7, 3.8000000000000003, 4.9]
xlabel_x: [0.25, 1.35, 2.45, 3.5500000000000003, 4.65]