
代码绘制成果展示











代码解释


第一部分

# =========================================================================================# ====================================== 1. 环境设置 =======================================# =========================================================================================import matplotlib.pyplot as pltimport pandas as pdimport numpy as np

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES = {1: ["#4B4CB5", "#4FB99F", "#901A34", "#E27D60", "#85DCBA", "#E8A87C"],}

第三部分

# =========================================================================================# ======================================3.绘图函数=======================================# =========================================================================================def plot_advanced_forest_chart(df, scheme_id, color_list):unique_algos = df['Algo'].unique() #分组color_map = {algo: color_list[i % len(color_list)] for i, algo in enumerate(unique_algos)} #映射颜色#创建画布fig, ax = plt.subplots(figsize=(8.4, 6.8))y_positions = np.arange(len(df)) #生成Y轴位置

第四部分

#遍历数据for i, row in df.iterrows():c_color = color_map[row['Algo']] #颜色#上面点ax.scatter(x, #xy, #ycolor=c_color, #颜色s=80, #大小edgecolors='white', #边框linewidth=0.5) #边框粗细

第五部分

if x > 0:#文本ax.text(-0.02, #xy, #yrow['CellType'], #文本ha='right', #水平va='center', #垂直color=c_color, #颜色fontsize=13) #字体大小#标记ax.text(x - 0.02, #xy, #yrow['Sig'], #文本ha='center', # 水平va='center', # 垂直color='#1976D2', #颜色fontsize=13, #字体大小fontweight='bold') #加粗

第六部分

#x=0辅助线ax.axvline(0, #xcolor='black', #颜色linewidth=2.5) #线宽max_x = df['SpearmanR'].abs().max() #X轴绝对值最大值ax.set_yticks(y_positions) #y轴刻度位置ax.set_yticklabels([]) #去掉y轴标注#遍历边框for spine in ax.spines.values():spine.set_color('#000000') #颜色spine.set_linewidth(2.5) #线宽

第七部分

ax.set_title("(a)", loc='left', fontsize=24, fontweight='bold') #子图编号# 设图例内容ax.legend(handles=legend_elements, #句柄loc='best', #位置frameon=False, #边框fontsize=16, #字体大小handletextpad=0.05) #间距

第八部分

# =========================================================================================# ======================================4.执行部分=======================================# =========================================================================================if __name__ == "__main__":df_real = pd.read_excel(r'data.xlsx') #读取数据scheme_id = 1selected_hex_colors = COLOR_SCHEMES[scheme_id]print('正在绘制并保存方案:', scheme_id)plot_advanced_forest_chart(df_real, scheme_id, selected_hex_colors)

如何应用到你自己的数据

1.设置原始数据的保存路径,执行部分:
df_real = pd.read_excel(r'data.xlsx') #读取数据2.设置是否要进行批量绘图,执行部分:
plot_all = True3.设置绘图函数的保存地址,绘图函数部分:
fig.savefig(fr'scheme_{scheme_id}.png', dpi=300, bbox_inches='tight')
推荐


获取方式
