• 环状分组柱状图 Python


    代码:

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. # 数据
    4. np.random.seed(123)
    5. group1 = 100 * np.random.rand(5)
    6. group2 = 100 * np.random.rand(5)
    7. group3 = 100 * np.random.rand(5)
    8. group4 = 100 * np.random.rand(5)
    9. groups = [group1, group2, group3, group4]
    10. group_names = list('ABCD')
    11. group_colors = ['#ff706d', '#7baf06', '#01bfc5', '#cb7bf6']
    12. # 画图
    13. fig = plt.figure(figsize=(8, 8), dpi=300, facecolor='white')
    14. ax = fig.add_subplot(projection='polar')
    15. ax.set_theta_zero_location("N")
    16. ax.set_theta_direction(-1)
    17. radii = [0]
    18. colors = ['white']
    19. for g, c in zip(groups, group_colors):
    20. radii.extend(g)
    21. colors.extend([c]*len(g))
    22. radii.append(0)
    23. colors.append('white')
    24. radii.pop()
    25. colors.pop()
    26. N = len(radii)
    27. r_lim = 180
    28. scale_lim = 100
    29. scale_major = 20
    30. bottom = 40
    31. theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
    32. width = 2 * np.pi / (N + 9)
    33. # 画出柱状图
    34. ax.bar(theta, radii, width=width, bottom=bottom, color=colors)
    35. # 画出刻度
    36. def scale(ax, bottom, scale_lim, theta, width):
    37. t = np.linspace(theta-width/2, theta+width/2, 6)
    38. for i in range(int(bottom), int(bottom+scale_lim+scale_major), scale_major):
    39. ax.plot(t, [i]*6, linewidth=0.25, color='gray', alpha=0.8)
    40. # 画出刻度值
    41. def scale_value(ax, bottom, theta, scale_lim):
    42. for i in range(int(bottom), int(bottom+scale_lim+scale_major), scale_major):
    43. ax.text(theta,
    44. i,
    45. f'{i-bottom}',
    46. fontsize=3,
    47. alpha=0.8,
    48. va='center',
    49. ha='center'
    50. )
    51. s_list = []
    52. g_no = 0
    53. aa = ["OA","AA","K","EA","HA"]
    54. for t, r in zip(theta, radii):
    55. if r == 0:
    56. s_list.append(t)
    57. if t == 0:
    58. scale_value(ax, bottom, t, scale_lim)
    59. else:
    60. scale(ax, bottom, scale_lim, t, width)
    61. else:
    62. t2 = np.rad2deg(t)
    63. # 标出每根柱的名称
    64. ax.text(t, r + bottom + scale_major*0.6,
    65. aa[g_no],
    66. fontsize=3,
    67. rotation=90-t2 if t < np.pi else 270-t2,
    68. rotation_mode='anchor',
    69. va='center',
    70. ha='left' if t < np.pi else 'right',
    71. color='black',
    72. clip_on=False
    73. )
    74. if g_no == (len(aa)-1):
    75. g_no = 0
    76. else:
    77. g_no += 1
    78. s_list.append(2 * np.pi)
    79. for i in range(len(s_list)-1):
    80. t = np.linspace(s_list[i]+width, s_list[i+1]-width, 50)
    81. ax.plot(t, [bottom-scale_major*0.4]*50, linewidth=0.5, color='black')
    82. ax.text(s_list[i]+(s_list[i+1]-s_list[i])/2,
    83. bottom-scale_major*1.2,
    84. group_names[i],
    85. va='center',
    86. ha='center',
    87. fontsize=4,
    88. )
    89. ax.set_rlim(0, bottom+scale_lim+scale_major)
    90. ax.axis('off')
    91. plt.show()

  • 相关阅读:
    2024华为OD机试真题- 计算三叉搜索树的高度-(C++/Python)-C卷D卷-100分
    高并发下的分布式缓存 | Read-Through缓存模式
    成都爱尔周进院长解读不同近视手术的不同度数要求
    国内市场上的 BI 软件到底有啥区别?
    proxmox PVE 安装 黑群晖
    【Web基础】Web应用体系结构 — 容器 + MVC设计模式
    vue3学习
    java split 末尾空值被截断了
    Spring Gateway使用JWT实现统一身份认证
    【React 源码】(五)React 应用的启动过程
  • 原文地址:https://blog.csdn.net/qq_45100200/article/details/133190941