
代码绘制成果展示














代码解释


第一部分

# =========================================================================================# ====================================== 1. 环境设置 =======================================# =========================================================================================import pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as plt

第二部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================COLOR_SCHEMES = {1: {'sites': ['#008B8B', '#87CEFA', '#FFA07A', '#FF0000'], 'corr_positive': 'blue',},}

第三部分

# =========================================================================================# ======================================4.绘图函数=======================================# =========================================================================================def create_network_scatterplot_matrix(df, color_scheme, save_path="."):#网格布局gs = fig.add_gridspec(total_rows, # 行8, # 列wspace=0.1, # 宽间距hspace=0.1, # 高间距left=0.05, # 左边距bottom=0.05, # 下边距right=0.95, # 右边距top=0.95) # 上边距#去掉轴标签ax.set_xlabel('')ax.set_ylabel('')# 是否是最后一行if i == n_bottom - 1 and j < n_bottom - 1:ax.xaxis.set_ticks_position('bottom'); # X刻度ax.xaxis.set_label_position('bottom') # X标签#设置刻度样式ax.tick_params(axis='x', # 轴which='major', # 主刻度labelsize=24, # 字号length=TICK_LENGTH, # 长度width=TICK_WIDTH) # 宽度else:ax.set_xticks([])# 是否是最左侧if j == 0 and i > 0:ax.yaxis.set_ticks_position('left'); # Y刻度ax.yaxis.set_label_position('left') # Y标签# 设置刻度样式ax.tick_params(axis='y', # 轴labelsize=24, # 字号length=TICK_LENGTH, # 长度width=TICK_WIDTH) # 宽度else:ax.set_yticks([])# 上三角if i < j:ax.set_visible(False) # 隐藏子

第四部分

bottom_matrix_node_pos = {} # 存放底部矩阵节点位置network_node_pos = {} # 存放网络节点位置ellipse_height_data = 0.024 # 定义节点高ellipse_width_data = ellipse_height_data * (8 / 16) * 1.5 # 定义节点宽# 定义节点样式node_style = {'marker': 'o', # 圆形'markersize': 25, # 大小'color': color_scheme['node'], # 颜色'zorder': 10, # 层'markeredgecolor': 'black', # 边框色'linestyle': 'None', # 无线'clip_on': False # 不裁剪}ax_main.text(label_pos[0], # xlabel_pos[1], # yf'${label}$', # 文本ha='left', # 水平va='center', # 垂直fontsize=34, # 字号fontweight='bold', # 加粗zorder=101, # 层color=color_scheme['legend_text']) # 文本颜色

第五部分

设置相关性影响方向的颜色映射。根据显著性的数值设置线型映射。根据相关系数绝对值设置线的粗细,相关系数绝对值越大线越粗。
arc_radius_base = -0.1 # 定义基础曲率半径# 遍历网络变量起点for var1 in network_vars:features_to_connect = scatter_vars_bottom # 连接散点目标变量dynamic_arc_radius = arc_radius_base # 原弧度control_offset = np.linalg.norm(dist_vec) * dynamic_arc_radius # 控制点偏移control_point = midpoint + perp_vec * control_offset # 控制点坐标verts = [pos1, control_point, pos2] # 控制点列表codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3] # 贝塞尔路径指令path = Path(verts, codes) # 构造路径对象# 创建曲线patch = PathPatch(path, # 路径对象facecolor='none', # 背景色edgecolor=color, # 轮廓颜色lw=linewidth, # 线粗linestyle=linestyle, # 线样式zorder=8, # 层alpha=0.9, # 透明度clip_on=False) # 不裁剪ax_main.add_patch(patch) # 添加到图上

第六部分

ax_legend = fig.add_subplot(gs[:, 6]) # 添加图例轴ax_legend.axis('off') # 隐藏图例边框current_y -= group_spacing # 间距#设置散点形状图例ax_legend.text(0.35, # xcurrent_y, # y'Leaf_type', # 标题fontdict=title_font_props, # 字体va='top') # 垂直current_y -= y_step * 1.2 # 更新ax_legend.plot([x_icon - 0.05, x_icon + 0.05], # x[current_y, current_y], # ycolor=color_scheme['corr_negative'], # 颜色lw=3, # 粗细clip_on=False) # 防裁剪ax_legend.plot([x_icon - 0.05, x_icon + 0.15], # xcurrent_y -= y_step # 更新ax_legend.plot([x_icon - 0.05, x_icon + 0.15], # x[current_y, current_y], # ycolor=color_scheme['legend_text'], # 线色dashes=(2, 2), # 虚线lw=4, # 粗细clip_on=False) # 防裁剪

第七部分

# =========================================================================================# ======================================2.颜色库=======================================# =========================================================================================if __name__ == '__main__': # 读取数据all_vars_to_check = network_vars + scatter_vars_bottom # 拼接列名scheme_id = 20selected_scheme = COLOR_SCHEMES[scheme_id]print('正在绘制并保存方案:', scheme_id)create_network_scatterplot_matrix(main_df, color_scheme=selected_scheme, save_path=output_directory)

如何应用到你自己的数据

1.设置原始数据的保存路径,执行部分:
data_file_path = r"data.xlsx" 2.设置绘图结果的保存路径,执行部分:
output_directory = r"分析网络图" # 输出目录3.设置是否要进行批量绘图,执行部分:
plot_all = True4.设置节点数据,基础变量设置部分:
network_vars = ['G_wmax', 'V_cmax', 'WUE_i'] 5.设置散点图要用到的变量数据,基础变量设置部分:
scatter_vars_bottom = ['Leaf N concentration', 'Chl concentration', 'SLA', 'Stomatal density', 'Pore length'] 6.设置颜色配置要用到的数据,基础变量设置部分:
sites = ['CB: Cold-temperate forest', 'DL: Warm-temperate forest', 'DH: Subtropical forest', 'XSBN: Tropical forest'] 7.设置形状设置要用到的数据,基础变量设置部分:
leaf_types = ['Deciduous', 'Evergreen']
往期内容
