如何创建多个一维轴,用彩色线条显示间隔?

How to create multiple 1D axes showing intervals with colored lines?

我想可视化数学域或区间。同样,我想可视化一个布尔数组。有多个这样的阵列,理想情况下是一个在另一个上方绘制。

我有一些数据:几段录音,比如 100 分钟。每个记录仅在部分时间满足给定条件。我想可视化每个录音的时间 "True"。一些更简单的变体:

在我的例子中,每个记录可以是多个间隔的并集。例如:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb

sb.set_context("paper")

times = np.arange(0, 100)

mask1 = (times >= 0) * (times <= 30) + (times >= 70) * (times <= 100)
mask2 = (times >= 20) * (times <= 80)

我可以分别绘制每个记录,用我写的这两个函数:

def bool2extreme(mask, times) :
    """return xmins and xmaxs for intervals in times"""
    binary = 1*mask
    slope = np.diff(binary)

    extr = (slope != 0)
    signs = slope[extr]

    mins = list(times[1:][slope==1])
    maxs = list(times[:-1][slope==-1])

    if signs[0]==-1:
        mins = [times[0]] + mins

    if signs[-1]==1:
        maxs = maxs + [times[-1]]

    return mins, maxs

def plot_interval(mask, times, y=0, color='k', ax=None) :

    if ax==None:
        print('None')
        ax = plt.gca()

    xmins, xmaxs = bool2extreme(mask, times)

    for xmin, xmax in zip(xmins, xmaxs):

        ax.plot([xmin, xmax], [y,y], lw=6, color=color)

    return ax

我的问题是控制各个区间之间的垂直间距。事实上,当我绘制其中一个时,有一个我不想要的垂直轴。即使我将它的可见性设置为 False,它仍然存在并占用 space。所以,当我把每个记录放在不同的子图上时,它们之间的垂直间距太大了:

masks = [mask1, mask2]
labels = ['domain1', 'domain2']
n_plots = len(masks)

fig, axs = plt.subplots(n_plots, sharex=True)

for i, mask in enumerate(masks) :

    axs[i] = plot_interval(mask, times, ax=axs[i])

axs[-1].set_xlabel('Time (min)')

sb.despine()

我尝试过的另一种选择是:让所有间隔都在同一轴上,但 y 值不同。但是区间之间垂直间距的问题还是一样

masks = [mask1, mask2]
labels = ['domain1', 'domain2']
n_plots = len(masks)

fig, ax = plt.subplots(sharex=True)

for i, mask in enumerate(masks) :

    ax = plot_interval(mask, times, y=i, ax=ax)


ax.set_xlabel('Time (min)')

ax.set_yticks(range(n_plots))
ax.set_yticklabels(labels)
ax.grid(axis="x")

sb.despine(left=True)

如何控制这些间隔之间的垂直间距?

一些想法:

  • 创建子图时高度较小的图形大小; figsize 的高度控制水平轴之间的距离:以英寸
  • 测量时,它们将 height/num_axes 分开
  • ax.yaxis.set_visible(False) 隐藏 y 轴上的刻度线
  • ax.spines['left'].set_color('None') 使 y 轴的脊柱不可见
  • ax.spines['bottom'].set_position(('data', 0)) 将 x 轴放置在 y=0 高度
  • (可选)ax.tick_params(labelbottom=True) 为所有子图中的 xticks 添加标签(而不是仅在最后一个)
  • 使用矩形而不是粗线可以更好地控制线的确切起点和终点以及轴上下的粗细
  • 为了控制矩形的高度,需要固定ylim;我提出(-1.5, .5),因此可以适当地选择厚度;下面还有更多 space 为 xticks
  • 的标签腾出空间
  • 由于绘制矩形不会自动更新 xlims,因此需要明确设置它们
  • (可选)ax.tick_params(which='both', direction='in') 获取上方而不是下方的刻度线(主要刻度和次要刻度)

要在左侧添加标签,以下方法对我有用:

# ax.yaxis.set_visible(False)  # removed, as it also hides the ylabel
ax.set_ylabel('my ylabel', rotation=0, ha='right', labelpad=10)
ax.set_yticks([])  # to remove the ticks, the spine was already removed 

在演示代码中,在末尾添加了更多的 xticks 和某种类型的箭头。演示中有 7 个掩码,以更好地查看轴之间的距离效果。尝试让轴尽可能靠近,0.4 英寸的距离似乎是可行的。 (bool2extreme 函数未受影响,因为它与用作输入的格式密切相关。)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Polygon
import matplotlib.ticker as plticker
import seaborn as sbs

sbs.set_context("paper")

times = np.arange(0, 101)
num_masks = 7
masks = [np.zeros_like(times, dtype=bool) for _ in range(num_masks)]
for i in range(num_masks):
    for j in range(50):
        masks[i] += (times >= (i+3)*j) * (times <= (i+3)*j+i+1)
masks = masks[::-1] # reverse to get the masks plotted from bottom to top

def bool2extreme(mask, times) :
    """return xmins and xmaxs for intervals in times"""
    binary = 1*mask
    slope = np.diff(binary)

    extr = (slope != 0)
    signs = slope[extr]
    mins = list(times[1:][slope==1])
    maxs = list(times[:-1][slope==-1])
    if signs[0]==-1:
        mins = [times[0]] + mins
    if signs[-1]==1:
        maxs = maxs + [times[-1]]
    return mins, maxs

def plot_interval(mask, times, xlim=None, y=0, thickness=0.4, color='k', ax=None):
    if ax is None:
        ax = plt.gca()
    ax.yaxis.set_visible(False)
    ax.spines['left'].set_color('None')
    ax.spines['right'].set_color('None')
    ax.spines['top'].set_color('None')
    ax.spines['bottom'].set_position(('data', 0))
    ax.tick_params(labelbottom=True)  # to get tick labels on all axes
    # ax.tick_params(which='both', direction='in')`  # tick marks above instead below the axis
    ax.xaxis.set_major_locator(plticker.MultipleLocator(base=10)) # major ticks in steps of 10
    ax.xaxis.set_minor_locator(plticker.MultipleLocator(base=1))  # minor ticks in steps of 1
    ax.set_ylim(-1.5,.5)
    if xlim is None:
        xlim = (times[0]-0.9, times[-1]+0.9)
    ax.set_xlim(xlim)
    xmins, xmaxs = bool2extreme(mask, times)
    for xmin, xmax in zip(xmins, xmaxs):
        #ax.add_patch(Rectangle((xmin, y-thickness), xmax-xmin, 2*thickness, linewidth=0, color=color))
        ax.add_patch(Rectangle((xmin, y), xmax-xmin, thickness, linewidth=0, color=color))
    triangle1 = [(xlim[0]-0.5, y), (xlim[0], y-thickness), (xlim[0], y+thickness)]
    ax.add_patch(Polygon(triangle1, linewidth=0, color='black', clip_on=False))
    triangle2 = [(xlim[1]+0.5, y), (xlim[1], y-thickness), (xlim[1], y+thickness)]
    ax.add_patch(Polygon(triangle2, linewidth=0, color='black', clip_on=False))
    return ax

n_plots = len(masks)
dist_between_axis_in_inches = 0.4

fig, axs = plt.subplots(n_plots, sharex=True, figsize=(10, dist_between_axis_in_inches*len(masks)))
for i, mask in enumerate(masks) :
    axs[i] = plot_interval(mask, times, xlim=(times[0]-0.5, times[-1]+0.5), ax=axs[i], color='lime')
axs[-1].set_xlabel('Time (min)')
plt.show()

轴靠近的结果:

PS: This post 包含更多关于添加箭头的建议。