
代码绘制成果展示












代码解释


第一部分

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

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES = {1: ['#314B76', '#FC9F5B', '#F3CC71', '#ED6D4E', '#008784', '#144249'],}

第三部分

# =========================================================================================# ======================================3.绘图函数=======================================# =========================================================================================def plot_advanced_forest_chart(df, scheme_id):total_plots = len(df) + 1 #子图数cols = int(np.ceil(np.sqrt(total_plots))) #列数rows = int(np.ceil(total_plots / cols)) #行数

第四部分

outer_circle_linewidth = 2.0 #外圈线宽max_radius_limit = 1.2 #最大半径#创建画布fig, axs = plt.subplots(rows, #行cols, #列subplot_kw={'projection': 'polar'}, #极坐标图figsize=(cols * 10 / 3, rows * 10 / 3)) #画布大小ax.grid(False) #关闭网格线ax.spines['polar'].set_visible(True) #外圈边框ax.spines['polar'].set_edgecolor('black') #外圈边框颜色ax.spines['polar'].set_linewidth(outer_circle_linewidth) #外圈线宽ax.set_ylim(0, max_radius_limit) #半径显示范围

第五部分

ax_leg = axs[0, 0] #取第一个子图作图例#设置图例ax_leg.bar(angles, #角度[1] * num_cats, #半径width=width, #扇形宽度bottom=0.0, #起始半径color=colors_to_use, #扇形颜色edgecolor='black', #边框颜色linewidth=1) #边框线宽

第六部分

positions = [] #初始化子图位置列表if vals[i] > 0:ax.bar(angles[i], #角度vals[i], #半径width=width, #宽度bottom=0.0, #起始半径color=colors_to_use[i], #颜色edgecolor='black', #边框颜色linewidth=1) #边框线宽

第七部分

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

如何应用到你自己的数据

1.设置原始数据的保存路径,执行部分:
excel_file = r'data.xlsx'2.设置是否进行批量绘图,执行部分:
plot_all = True3.设置非类别列的类名,绘图函数部分:
categories = [col for col in df.columns if col != 'Bundle'] #类别名4.设置绘图结果的保存地址,绘图函数部分:
plt.savefig(fr'scheme_{scheme_id}.png', dpi=300, bbox_inches='tight')
推荐


获取方式
