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

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

我只有一些数据:在一段100分钟的时间内录制了几张唱片。每个记录仅在部分时间内满足给定条件。我想可视化每个记录为“ True”的时间。一些更简单的变体:

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

就我而言,每个记录可以是多个间隔的并集。例如:

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,它也存在并占用空间。因此,当我将每个记录放在不同的子图上时,它们之间的垂直间距太大:

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()

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

我尝试的另一种选择是:将所有间隔都设置在同一轴上,但是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)

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

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

参考方案

一些想法:

创建子图时,figsize的高度较小; 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)以便可以适当选择厚度。在下面留出更多空间来放置标签的标签
由于绘制矩形不会自动更新xlim,因此需要明确设置它们
(可选)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()

轴靠在一起的结果:

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

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

单行的'if'/'for'语句是否使用Python样式好? - python

我经常在这里看到某人的代码,看起来像是“单线”,这是一条单行语句,以传统的“if”语句或“for”循环的标准方式执行。我在Google周围搜索,无法真正找到可以执行的搜索类型?任何人都可以提出建议并最好举一些例子吗?例如,我可以一行执行此操作吗?example = "example" if "exam" in exam…

为什么使用'=='或'is'比较字符串有时会产生不同的结果? - python

我有一个Python程序,其中将两个变量设置为'public'值。在条件表达式中,我有比较var1 is var2失败,但如果将其更改为var1 == var2,它将返回True。现在,如果我打开Python解释器并进行相同的“是”比较,则此操作成功。>>> s1 = 'public' >>…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

python JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…