
代码绘制成果展示












代码解释


第一部分

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

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES = {1: ['#E74C3C', '#3B4A6B', '#1C8C85'],}

第三部分

# =========================================================================================# ======================================3.绘图函数=======================================# =========================================================================================def plot_residual_stacked_histogram(df_real, scheme_id):for i, src in enumerate(sources):df_src = df_real[df_real['Source'] == src] #提取数据res_list.append(df_src['Residual'].values) #添加残差colors_list.append(selected_hex_colors[i % len(selected_hex_colors)]) #分配颜色

第四部分

# 创建画布fig, ax = plt.subplots(figsize=(7.2, 6))n, bins_out, patches = ax.hist(res_list, #数据bins=bins, #区间stacked=False, #不堆叠edgecolor='white', #边框颜色linewidth=0.8, #边框线宽rwidth=0.8) #柱状图宽度

第五部分

# 遍历每组柱子for i, container inenumerate(patches):im = ax.imshow(gradient_data, #数据extent=[x0, x0 + w, y0, y0 + h], #设置填充区域aspect='auto', #自动调整纵横比cmap=cmap_gradient, #渐变色origin='lower', #原点alpha=0.85, #透明度zorder=2) #层

第六部分

ax.set_xlim(-res_limit, res_limit) #x轴范围max_height = np.max([np.max(hist_val) for hist_val in n]) #最大高度ax.set_ylim(0, max_height * 1.45) #y轴范围#设置轴刻度标注大小#添加x轴刻度标签ax.set_xticklabels(group_labels, #文本rotation=45, #旋转ha='center', #水平fontsize=10 #大小)

第七部分

# ========================================================================================# ======================================4.执行部分=======================================# =========================================================================================if __name__ == "__main__":excel_input_path = r'data.xlsx'df_real = pd.read_excel(excel_input_path) #读取数据scheme_id = 1print(f'正在绘制并保存方案:{scheme_id}')plot_residual_stacked_histogram(df_real, scheme_id)

如何应用到你自己的数据

1.设置原始数据的保存路径,执行部分:
excel_input_path = r'\data.xlsx'2.设置是否要进行批量绘图,执行部分:
plot_all = True #设置批量绘图3.提取数据,绘图函数部分:
residuals = df_real['Residual'].values4.设置绘图结果的保存地址,绘图函数部分:
plt.savefig(fr'Scheme_{scheme_id}.png', dpi=300,bbox_inches='tight')
推荐


获取方式
