重叠测试和可视化

问题描述 投票:0回答:1

我有以下数据框:

    window_start    window_end  dataset
29125   1828457 1828868 129C
29126   1891493 1891904 129C
29127   2312557 2312968 129C
29128   3745905 3746316 129C
29129   5036701 5037112 129C
... ... ... ...
49838   185443673   185444084   172C
49840   186261905   186262316   172C
49841   186888969   186889380   172C
49980   187896721   187897132   172C
49987   190067549   190067960   172C
530 rows × 3 columns

我希望得到两个结果:1.在所有区间上以数字方式识别重叠区域(例如[1828450、1828860]等); 2. 使用与我在下面报告的类似的 matplot 图可视化所有间隔。

enter image description here

我已经尝试过以下代码来解决第2点,但它没有显示任何内容:

x_start_df = AllC_chr1[AllC_chr1.dataset=='129C'].window_start
xstart = x_start_df.to_numpy()
x_end_df   = AllC_chr1[AllC_chr1.dataset=='129C'].window_end
xstart = x_end_df.to_numpy()
y       = AllC_chr1[AllC_chr1.dataset=='129C'].index
pl.figure()
pl.barh(y/1000, width=x_end-x_start, left = x_start)

欢迎任何建议。

感谢您的支持

matplotlib overlapping horizontal-line
1个回答
0
投票

主要问题是垂直条的宽度与条之间的距离相比非常小。这样,您只能看到条形的轮廓,而看不到其内部。您可以将默认的白色边缘颜色更改为其他颜色。

您可以使用

'dataset'
列作为 y 轴,以自动标记它们。条形图是用“粘性边缘”绘制的(将左边距设置为零)。如果不需要,可以关闭
ax.use_sticky_edges

使用matplotlib,强烈推荐

import matplotlib.pyplot as plt
,使代码更容易与示例代码进行比较(也让其他人更快地理解代码)。此外,面向对象的界面有助于更轻松地理解正在发生的事情。

import matplotlib.pyplot as plt
import pandas as pd

AllC_chr1 = pd.DataFrame({
    'window_start': [1828457, 1891493, 2312557, 3745905, 5036701, 185443673, 186261905, 186888969, 187896721,
                     190067549],
    'window_end': [1828868, 1891904, 2312968, 3746316, 5037112, 185444084, 186262316, 186889380, 187897132, 190067960],
    'dataset': ['129C', '129C', '129C', '129C', '129C', '172C', '172C', '172C', '172C', '172C']},
    index=[29125, 29126, 29127, 29128, 29129, 49838, 49840, 49841, 49980, 49987])

df = AllC_chr1
# df = AllC_chr1 [AllC_chr1['dataset']=='129C']

fig, ax = plt.subplots()
ax.barh(df['dataset'], left=df['window_start'],
        width=df['window_end'] - df['window_start'], edgecolor='blue')
# Disable sticky edges
ax.use_sticky_edges = False
# Set the x-axis tick labels to millions
ax.xaxis.set_major_formatter(lambda x, pos: f"{x / 1000000:g}M")

plt.tight_layout()
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.