4.26留3

4.26留

import matplotlib.pyplot as plt import numpy as np # 年龄分组 age_groups = [&#39;<40&#39;, &#39;40-49&#39;, &#39;50-59&#39;, &#39;60-69&#39;, &#39;70+&#39;] # 状态数据(只有3种状态) state_data = { 0: [2.34, 4.26, 4.61, 3.01, 2.08], # 状态0 2: [78.91, 74.04, 73.78, 69.32, 64.98], # 状态2 4: [18.75, 21.7, 21.6, 27.67, 32.94] # 状态4 } # 颜色配置(3种颜色) colors = [&#39;#4e79a7&#39;, &#39;#e15759&#39;, &#39;#59a14f&#39;] state_labels = [&#39;0&#39;, &#39;1 and 2&#39;, &#39;3 and 4&#39;] # 设置图表 plt.figure(figsize=(12, 8)) bar_width = 0.25 x = np.arange(len(age_groups)) # 绘制分组柱状图并添加数值标签 for i, state in enumerate([0, 2, 4]): # 绘制柱子 bars = plt.bar(x + i * bar_width, state_data[state], width=bar_width, color=colors[i], label=state_labels[i]) # 在每个柱子上方添加数值标签 for bar in bars: height = bar.get_height() plt.text(bar.get_x() + bar.get_width() / 2., height + 0.5, # 在柱子顶部上方0.5单位处添加标签 f&#39;{height:.1f}%&#39;, # 格式化显示一位小数 ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=9) # 添加标签和标题 plt.xlabel(&#39;Age (years)&#39;, fontsize=14, labelpad=15) plt.ylabel(&#39;Proportion (%)&#39;, fontsize=14, labelpad=15) plt.xticks(x + bar_width, age_groups, fontsize=12) plt.yticks(fontsize=12) # 设置Y轴范围,为数值标签出空间 plt.ylim(0, max(max(state_data[0]), max(state_data[2]), max(state_data[4])) * 1.15) # 添加图例(添加标题) plt.legend(fontsize=12) # 调整布局 plt.tight_layout() plt.savefig(&#39;age_state_distribution.tiff&#39;, dpi=600, format=&#39;tiff&#39;, bbox_inches=&#39;tight&#39;) # 确保标签完整保存 plt.show() 要无衬线字体
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值