You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制序列覆盖图?已知序列区间无需实际比对

生成序列覆盖图的实现方案

要实现你需要的序列覆盖图(支持分段区间展示),用matplotlib(Python)或ggplot2(R)都能轻松解决,替代普通条形图来处理区间中断的问题,下面是具体实现:

Python 实现(Matplotlib)

通过绘制分段矩形来展示每个序列的区间,完美适配中断的片段:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

# 定义你的序列区间数据,替换成实际的区间即可
sequence_intervals = {
    "主序列": [(1, 50)],  # 主序列的完整范围
    "T1": [(7, 12), (32, 36)],
    "T2": [(3, 8), (20, 25), (40, 48)]
}

# 创建画布和轴对象
fig, ax = plt.subplots(figsize=(12, 4))
# 给每个序列分配y轴位置
y_pos = {seq: idx for idx, seq in enumerate(sequence_intervals.keys())}
bar_height = 0.6  # 每个序列条的高度

# 遍历绘制每个序列的所有区间
for seq, intervals in sequence_intervals.items():
    y = y_pos[seq]
    for start, end in intervals:
        # 绘制水平矩形:(x起始坐标, y起始坐标, 宽度, 高度)
        rect = Rectangle(
            (start, y - bar_height/2),
            end - start,
            bar_height,
            facecolor='#1f77b4' if seq == "主序列" else '#ff7f0e',
            edgecolor='black',
            alpha=0.8
        )
        ax.add_patch(rect)
        # 可选:给每个区间添加位置标签
        ax.text((start+end)/2, y, f"{start}-{end}", ha='center', va='center', fontsize=10)

# 调整坐标轴样式
ax.set_yticks(list(y_pos.values()))
ax.set_yticklabels(list(y_pos.keys()))
ax.set_xlabel("序列位置")
ax.set_title("序列覆盖图")
# 设置x轴范围,预留一点边距
max_pos = max(end for intervals in sequence_intervals.values() for _, end in intervals)
ax.set_xlim(0, max_pos + 2)
ax.set_ylim(-0.5, len(sequence_intervals) - 0.5)

plt.tight_layout()
plt.show()

R 实现(ggplot2)

用geom_rect绘制分段矩形,同样可以处理中断区间:

library(ggplot2)

# 构造数据框,替换成你的实际区间数据
sequence_df <- data.frame(
  seq = c(rep("主序列", 1), rep("T1", 2), rep("T2", 3)),
  start = c(1, 7, 32, 3, 20, 40),
  end = c(50, 12, 36, 8, 25, 48),
  y = c(0, 1, 1, 2, 2, 2)
)

ggplot(sequence_df, aes(y = y)) +
  # 绘制分段矩形
  geom_rect(
    aes(xmin = start, xmax = end, ymin = y - 0.3, ymax = y + 0.3, fill = seq),
    color = "black", alpha = 0.8
  ) +
  # 设置y轴标签
  scale_y_continuous(breaks = c(0, 1, 2), labels = c("主序列", "T1", "T2")) +
  # 设置标题和坐标轴标签
  labs(x = "序列位置", y = "", title = "序列覆盖图") +
  # 自定义颜色
  scale_fill_manual(values = c("主序列" = "#1f77b4", "T1" = "#ff7f0e", "T2" = "#2ca02c")) +
  theme_minimal()

这两种方法都能精准将每个序列的区间对应到主序列的位置上,解决普通条形图无法展示中断区间的问题,你可以根据自己熟悉的编程语言选择实现。

内容的提问来源于stack exchange,提问作者FastaGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:40:33