python实现绘制多组并列条形图
实现功能:
python实现绘制多组并列条形图
输入多个类别的多个特征值,即一个每个种类都有几种特征,在同一个条形图中绘制出这几个种类的所有特征值,用于对比分析。
实现代码:
1 | import matplotlib.pyplot as plt |
2 | import numpy as np |
3 | import seaborn as sns |
4 | |
5 | def Draw_Parallel_bar(features,L1,L2,L3,L4,L5): |
6 | features=features |
7 | RA=L1;SS=L2;SLE=L3;Arthritis=L4;CTD=L5 |
8 | |
9 | bar_width = 0.1 |
10 | index_RA = np.arange(len(features)) |
11 | index_SS = index_RA + bar_width |
12 | index_SLE = index_SS + bar_width |
13 | index_Arthritis = index_SLE + bar_width |
14 | index_CTD = index_Arthritis + bar_width |
15 | |
16 | font = {'family': 'Times New Roman', |
17 | 'size': 12, |
18 | } |
19 | sns.set(font_scale=1.2) |
20 | plt.rc('font',family='Times New Roman') |
21 | |
22 | plt.bar(index_RA, height=RA, width=bar_width, color='peru', label='RA') |
23 | plt.bar(index_SS, height=SS, width=bar_width, color='g', label='SS') |
24 | plt.bar(index_SLE, height=SLE, width=bar_width, color='y', label='SLE') |
25 | plt.bar(index_Arthritis, height=Arthritis, width=bar_width, color='c', label='OA') |
26 | plt.bar(index_CTD, height=CTD, width=bar_width, color='violet', label='CTDs') |
27 | |
28 | plt.legend(fontsize = 12) # 显示图例 |
29 | plt.xticks(index_RA + 10*bar_width/5, features,fontsize = 12) |
30 | plt.xlabel('Autoantibodies',fontsize = 14) |
31 | plt.ylabel('Positive rate',fontsize = 14) |
32 | plt.show() |
33 | |
34 | if __name__=="__main__": |
35 | Draw_Parallel_bar(('CCP', 'MCV', 'AKA', 'RF', 'ANA','CRP','ESR'), |
36 | [0.9265,0.8840,0.4816,0.8779,0.6167,0.6012,0.8562], |
37 | [0.1860,0.3256,0.1163,0.7674,0.7442,0.2558,0.5814], |
38 | [0.1905,0.3810,0.0476,0.6667,0.8571,0.3333,0.8571], |
39 | [0.4211,0.7368,0.2105,0.6316,0.5789,0.4737,0.7368], |
40 | [0.2308,0.3077,0.0769,0.8462,0.8468,0.3846,0.7692]) |
实现效果:
喜欢记得点赞,转发,收藏,
关注V订阅号:数据杂坛,获取完整代码和效果,将持续更新!
请先 后发表评论~