gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:29:55 26 4
gpt4 key购买 nike

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

我拥有的是一些数据:多段录音,比如 100 分钟。每个记录仅在部分时间满足给定条件。我想形象化每个录音都是“真实”的时间。一些更简单的变体:

1D axes example

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

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

enter image description here

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

enter image description here

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

最佳答案

一些想法:

  • 创建子图时使用较小高度的图形大小; 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) 为所有子图中的 xtick 设置标签(而不是仅在最后一个)
  • 使用矩形而不是粗线可以更好地控制线的确切起点和终点以及轴上下的粗细
  • 为了控制矩形的高度,需要固定ylims;我提出(-1.5,.5),因此可以适本地选择厚度;下面有更多空间为 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()

轴靠近的结果:

resulting plot

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

关于python - 如何创建多个一维轴,用彩色线条显示间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com