
代码绘制成果展示










代码解释


第一部分

# =========================================================================================# ====================================== 1. 环境设置 =======================================# =========================================================================================import matplotlib.pyplot as pltimport numpy as npimport pandas as pdplt.rcParams['font.family'] = 'serif'plt.rcParams['font.serif'] = ['Times New Roman']plt.rcParams['axes.unicode_minus'] = Falseimport matplotlibmatplotlib.rcParams['pdf.fonttype'] = 42matplotlib.rcParams['ps.fonttype'] = 42

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_LIBRARY = {1: ['#A6CEE3', '#1F78B4', '#B2DF8A', '#33A02C', '#FB9A99', '#E31A1C', '#FDBF6F', '#FF7F00', '#CAB2D6', '#6A3D9A', '#FFFF99', '#B15928'],}SCHEME_ID = 60 #使用的配色方案selected_colors = COLOR_LIBRARY.get(SCHEME_ID, COLOR_LIBRARY[1]) #获取颜色

第三部分

# =========================================================================================# ======================================3.绘图函数=======================================# =========================================================================================def plot_gene_polar_chart(genes, data, conditions, group_colors):num_genes = len(genes) #获取最外圈名称num_conds = len(conditions) #获取柱子数量inner_radius = 0.35 #中间空白半径total_circumference = 2 * np.pi #圆周总弧度sector_angle = total_circumference / num_genes #每个区域的扇形角度gap_ratio = 0.25 #区域之间的间隙比例group_angle_width = sector_angle * (1 - gap_ratio) #实际绘图区域的角度宽度bar_width = group_angle_width / num_conds #柱子的宽度centering_offset = (sector_angle * gap_ratio) / 2 #计算偏移量,使柱子组居中theta = group_start + centering_offset + j * bar_width + bar_width / 2 #当前柱子的中心角度group_thetas.append(theta) #将计算好的角度保存thetas.append(group_thetas) #保存当前区域的所有柱子角度gene_center = group_start + sector_angle / 2 #计算该区域扇形区域的中心角度,用于放置标签gene_centers.append(gene_center) #保存中心角度

第四部分

#创建画布fig = plt.figure(figsize=(10, 10), dpi=150)#添加子图,设置为极坐标投影ax = fig.add_subplot(111, projection='polar')#遍历每一个刻度值for g in grid_values:#绘制背景网格线ax.plot(np.linspace(0, 2 * np.pi, 200), # 生成0到2π的点[inner_radius + g] * 200, #生成对应的半径值color='gray', #颜色linestyle=':', #样式linewidth=0.8, #宽度alpha=0.5, #透明度zorder=0) #顺序

第五部分

#再次遍历每一个区域进行绘图for i in range(num_genes):#获取颜色color = group_colors[i % len(group_colors)]vals = data[i] #获取当前区域对应的数据值angles = thetas[i] #获取当前区域对应的柱子角度label_r = inner_radius + 1.08 #柱子上方标签的半径位置deg = np.degrees(angles[j]) #将弧度转换为角度standard_deg = 90 - deg #转换角度坐标系if 90 < deg < 270:rot = standard_deg + 180 #旋转角度va = 'center' #对齐方式else:rot = standard_deg #旋转角度va = 'center' #对齐方式#添加文本ax.text(angles[j], #角度label_r, #半径conditions[j], #内容rotation=rot, #旋转角度ha='center', #水平居中va=va, #垂直居中fontsize=14, #字体大小color='#444444') #颜色name_r = arc_r + 0.15 #计算区域名称标签的半径位置deg_center = np.degrees(center) % 360 #将中心弧度转换为度角度if 90 < deg_center < 270:name_rot = -deg_center + 180 #旋转角度adj_r = name_r - 0.05 #半径else:name_rot = -deg_center #旋转角度adj_r = name_r #半径#弧线处文本ax.text(center, #中心角度adj_r, #半径genes[i], #文本内容rotation=name_rot, #旋转角度ha='center', #水平居中va='center', #垂直居中fontsize=18, #字体大小color=color) #颜色

第六部分

# =========================================================================================# ======================================4.执行部分=======================================# =========================================================================================if __name__ == "__main__":excel_filename = r"data.xlsx" #数据路径df_read = pd.read_excel(excel_filename) #读取genes_plot = df_read['Gene'].tolist() #最外圈名称conditions = ['S', 'SP', 'W', 'WP'] #柱子数据列data_plot = df_read[conditions].values.tolist() #提取数据并转换为列表#调用绘图函数plot_gene_polar_chart(genes_plot, data_plot, conditions, selected_colors)

如何应用到你自己的数据

1.设置配色:
SCHEME_ID = 60 #使用的配色方案2.设置绘图结果的保存地址:
plt.savefig(fr'result_chart{SCHEME_ID}.png', dpi=300, bbox_inches='tight')3.设置绘图结果的保存地址:
excel_filename = r"\data.xlsx" #数据路径4.提取数据:
genes_plot = df_read['Gene'].tolist() #最外圈名称conditions = ['S', 'SP', 'W', 'WP'] #柱子数据列

推荐


获取方式
