pyplot 并列柱状图

# -*- 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]

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值