
代码绘制成果展示













代码解释


第一部分

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

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES = {1: {'c1_fill': '#339DB5', 'c1_edge': '#267B8E', 'c2_fill': '#C9342B', 'c2_edge': '#9C2821', 'c3_fill': '#EBA11F', 'c3_edge': '#BB8119'},}

第三部分

# =========================================================================================# ======================================3.绘图函数=======================================# =========================================================================================def plot_grouped_barchart(df, scheme_id=1):base_val = df.iloc[:, 1]base_err = df.iloc[:, 2]aug_val = df.iloc[:, 3]aug_err = df.iloc[:, 4]boot_val = df.iloc[:, 5]boot_err = df.iloc[:, 6]

第四部分

num_plots = len(regions) #子图数cols = 5 #组图列rows = math.ceil(num_plots / cols) #行#创建画布fig, axes = plt.subplots(rows, #行cols, #列figsize=(cols * 3.5, rows * 3.5)) #尺寸

第五部分

bar_width = 0.15 #柱子宽intra_group_gap = 0.19 #柱子之间中心间距pos2 = 0 #中间主子x坐标pos1 = - (bar_width + intra_group_gap) #左柱子xpos3 = + (bar_width + intra_group_gap) #右柱子xlegend_bars = [] #用于收集用于生成图例的柱子绘图对象

第六部分

#遍历绘图for i in range():ax.grid(axis='y', #ycolor='gray', #颜色linestyle='--', #样式linewidth=0.8, #线宽alpha=0.3, #透明度zorder=0) #层#第一根柱子b1 = ax.bar(pos1, #xbase_val.iloc[i], #高度bar_width, #宽度yerr=base_err.iloc[i], #误差棒capsize=4, #误差棒两端横线color=mcolors.to_rgba(c1_fill, # 填充颜色alpha=1), #透明度

第七部分

#此子图最高y坐标local_max = max(base_val.iloc[i] + base_err.iloc[i],aug_val.iloc[i] + aug_err.iloc[i],boot_val.iloc[i] + boot_err.iloc[i])offset = local_max * 0.1 #设置偏移量,顶部留下一些空白#y轴标题ax.set_ylabel(r'$R^2$', #文本fontsize=16, #字体大小fontweight='bold', #加粗labelpad=5) #间距

第八部分

#调整布局plt.tight_layout(rect=[0, 0.08, 1, 1])#图例fig.legend(legend_bars, #句柄model_names, #文本fontsize=22, #字体大小loc='lower center', #位置bbox_to_anchor=(0.5, 0.02), #坐标ncol=3, #列frameon=False) #去掉边框

第九部分

# =========================================================================================# ======================================4.主程序执行=======================================# =========================================================================================if __name__ == '__main__':excel_file_path = r'data.xlsx' #原始数据df = pd.read_excel(excel_file_path) #读取#设置是否批量绘图plot_all = Trueif plot_all:for scheme_id in COLOR_SCHEMES.keys():plot_grouped_barchart(df, scheme_id=scheme_id)print('正在绘制并保存方案:', scheme_id)else:scheme_id = 22plot_grouped_barchart(df, scheme_id=scheme_id)print('正在绘制并保存方案:', scheme_id)

如何应用到你自己的数据

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


获取方式
