Skip to content

Redefine the plot_points function in lefse_plot_cladogram.py to beautify the output results. And solve the shadow bug issue. #36

@LianheCai078

Description

@LianheCai078

重新定义plot_points函数,美化输出结果。以及解决阴影bug问题。

def plot_points(father, params, pt_scale, ax):
"""
递归绘制分类树图中的节点。

该函数从给定的父节点开始遍历树结构,
对其子节点进行排序并递归绘制,然后如果当前节点可见,
则绘制当前节点。节点的显示根据其显著性进行调整
(颜色 'y' 表示非显著节点)。

参数:
- father: 当前树节点对象。
- params: 绘图参数字典(例如 'fore_color', 'markeredgewidth', 'alpha')。
- pt_scale: 用于缩放节点大小的列表或元组(例如 [基本大小, 缩放因子])。
- ax: Matplotlib 的轴对象,用于绘图。

返回:
- (x, r): 当前节点的坐标位置(极坐标:角度 x, 半径 r)。
"""

# 获取当前节点的子节点
children = father.get_children()

# 对子节点进行排序:优先考虑显著节点(非 'y' 颜色),按丰度降序排列
# 非显著节点 ('y') 通过负丰度排序置于最后
children.sort(key=lambda a: -int(a.get_color() == 'y') * a.abundance)

# 提取当前节点的坐标位置(可能为极坐标:角度 x, 半径 r)
x, r = father.pos[0], father.pos[1]

# 递归绘制每个子节点
for i, child in enumerate(children):
    xc, rc = plot_points(child, params, pt_scale, ax)

# 如果节点不可见,则跳过绘制并返回其位置
if not father.viz:
    return x, r

# 根据节点显著性调整颜色:
# - 对于非显著节点 ('y'):边缘使用前景色,填充为白色
# - 对于显著节点:边缘和填充均使用节点的颜色
if father.get_color() == 'y':
    edge_color = params['fore_color']
    fill_color = "white"
else:
    edge_color = father.get_color()
    fill_color = father.get_color()

# 定义缩放因子,用于调整节点大小(此处设置为 0.1 以缩小非显著节点)
scale_factor = 0.1

# 计算节点大小 (ps):基本大小 + 缩放后的丰度 + 调整后的基本大小
# 这会根据丰度使节点变大,并通过 scale_factor 进行缩小调整
ps = pt_scale[0] + father.abundance / pt_scale[1] + pt_scale[0] * scale_factor

# 计算标记边缘宽度 (pw):
# - 对于白色填充(非显著)节点:使用较薄宽度
# - 对于有色(显著)节点:使用较厚宽度(3 倍)
pw = params['markeredgewidth'] if fill_color == "white" else params['markeredgewidth'] * 3.0

# 以圆形 ('o') 绘制节点:
# - 对于根/中心节点 (x=0, r=0):使用最小边缘宽度
# - 对于其他节点:使用计算的 pw
# markerfacecolor 设置填充颜色,markeredgecolor 和 markeredgewidth 设置边框
if x == 0 and r == 0:
    ax.plot(x, r, 'o', markersize=ps, markerfacecolor=fill_color, markeredgewidth=0.01, markeredgecolor=edge_color)
else:
    ax.plot(x, r, 'o', markersize=ps, markerfacecolor=fill_color, markeredgewidth=pw, markeredgecolor=edge_color)

# 返回节点的坐标位置
return x, r

阴影bug:
源代码
x.bar(fr_0, clto, width = fr_1-fr_0, bottom = float(l-1)/float(de), alpha = params['alpha'], color=col, edgecolor=col)

修改后
ax.bar(fr_0, clto, width = fr_1-fr_0, bottom = float(l-1)/float(de), align='edge', alpha = params['alpha'], color=col, edgecolor=col)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions