
代码绘制成果展示











代码解释


第一部分

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

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES= {1: {'t1_f': '#d0bfe6', 't1_e': '#8f71be', 't2_f': '#fbb2c6', 't2_e': '#ee6488', 'bg_l': '#f7f2fc', 'bg_r': '#fcf0f3', 'm1': '#a793c9', 'm2': '#f598a9'},}

第三部分

# =========================================================================================# ======================================3.哑铃图绘制函数=======================================# =========================================================================================def draw_dumbbell_plot_refined(labels, t1_vals, t2_vals, diffs,scheme_id=60):y_pos = np.arange(len(labels))[::-1] #生成y轴坐标序列,将其倒序,确保第一个标签显示在图表最上方# 创建线性渐变颜色映射,用于连接数据点的线段cmap_gradient = LinearSegmentedColormap.from_list("mycmap", [color_t1_fill, color_t2_fill])

第四部分

#创建画布fig, ax = plt.subplots(figsize=(10, 8))xlim_min, xlim_max = 0, 65 #定义x轴范围ylim_min, ylim_max = -1, len(labels) #y轴范围aspect='auto', #纵横比cmap=cmap_bg, #渐变色映射extent=[xlim_min, #左边界xlim_max, #右边界ylim_min, #下边界ylim_max], #上边界

第五部分

#计算平均值mean_t1 = np.mean(t1_vals)mean_t2 = np.mean(t2_vals)# 绘制右侧均值标注ax.text(mean_t2, #xylim_max - 0.2, #yf'Mean: {mean_t2:.1f}', #文本color=color_t2_edge, #颜色ha='center', #水平va='top', #垂直fontsize=12, #大小为fontweight='bold', #加粗zorder=4) #层

第六部分

# 绘制左侧点ax.scatter(t1_vals, #xy_pos, #ys=600, #大小c=color_t1_fill, #内部填充颜色edgecolors=color_t1_edge, #边缘颜色linewidths=2.5, #线宽zorder=3, #层label='Treatment 1') #图例标签名称# 绘制右侧点ax.scatter(t2_vals, #xy_pos, #ys=600, #大小设c=color_t2_fill, #填充颜色edgecolors=color_t2_edge, #边缘颜色linewidths=2.5, #线宽zorder=3, #层label='Treatment 2') #图例标签名称

第七部分

#遍历每一行数据,用于添加数值for i in range(len(labels)):ax.text(t1_vals[i], #xy_current + 0.3, #ystr(t1_vals[i]), #文本ha='center', #水平va='bottom', #垂直color=color_t1_edge, #颜色fontsize=14, #大小fontweight='bold') #加粗#添加差值的文本ax.text(mid_x, #xy_current - 0.2, # ydiffs[i], #文本ha='center', #水平va='top', #垂直color='black', #颜色fontsize=13, #大小fontweight='bold') #加粗

第八部分

handles, legend_labels = ax.get_legend_handles_labels() # 获取图例的句柄和对应的标签文本#生成图例leg = ax.legend(handles, #句柄leg.get_frame().set_edgecolor('black') #图例边框颜色leg.get_frame().set_linewidth(1.0) #图例边框线宽leg.get_frame().set_alpha(0.9) #图例背景边框透明度#主标题ax.set_title('Changes of Clinical Features Under Two Treatments', #文本fontsize=18, #大小fontweight='bold', #加粗pad=25) #距离

第九部分

# =========================================================================================# ======================================4.颜色库=======================================# =========================================================================================if __name__ == '__main__':df = pd.read_excel(r'data.xlsx') #读取原始数据#批量绘图if plot_all:#遍历for i in range(1, 61):if i in COLOR_SCHEMES:#调用绘图函数draw_dumbbell_plot_refined(labels, t1_vals, t2_vals, diffs, scheme_id=i)#绘制单图else:#要使用的配色方案target_scheme = 22#调用绘图函数draw_dumbbell_plot_refined(labels, t1_vals, t2_vals, diffs, scheme_id=target_scheme)

如何应用到你自己的数据

1.设置原始数据路径:
df = pd.read_excel(r'data.xlsx') #读取原始数据2.读取y轴名称数据:
labels = df['Clinical Feature'].tolist()#提取Y轴标签3.读取左侧数值数据:
t1_vals = df['Treatment 1'].tolist()#提取左侧数据4.读取右侧数值数据:
t2_vals = df['Treatment 2'].tolist()#提取右侧数据5.设置是否要进行批量绘图:
plot_all = True6.设置绘图结果的保存地址:
plt.savefig(fr'\plot{scheme_id}.png', dpi=300, bbox_inches='tight')
推荐


获取方式
