选择在图表的 "edge" 上绘制线(并循环),而不是跨越图表
Choose to plot the line over the "edge" of the graph instead (and loop around), instead of across the graph
所以,我有一张键的二面角图。 y 轴仅从 0-360,x 轴是帧(想想时间步长)。如果值超过 360,我需要绘图“循环”回零,并绘制两点之间的最短距离(如果需要越过图形的边缘并“循环”回而不是穿过图形).
d3的图看起来还可以,但实际上需要跳过图的边缘而不是越过图的边缘。
d5 的情节有一个严重的问题,对于一个小的旋转有一个巨大的跳跃只是因为它恰好刚好低于 0 度。
我希望这两个图都向底部绘制(向零)并重新出现在图的顶部,有效地选择数据点之间的最短距离。我不希望涉及图的翻译的解决方案来删除这些人工制品(它有效,我已经做到了,但你失去了关于角度真实值的信息)。可以绘制“零以下”(因此 y 轴从 300 到 360|0 到 200 到 300)的解决方案也很棒。使用其他库的解决方案非常好。如果需要我可以提供数据集。
我希望它执行的操作示例(绿线)
我曾尝试寻找类似的解决方案,但无济于事。关于周期性边界的问题使用 numpy 数据集掩码来隐藏某些跳跃,但它们具有连续函数(我的是“跳跃”)。
感谢您的帮助,我将不胜感激。
数据集(使它们比图表上的小一点,只保留跳过):
D3:
x = [41.0, 43.0, 45.0, 47.0, 49.0, 51.0, 53.0, 55.0, 57.0, 59.0, 61.0, 63.0, 65.0, 67.0, 69.0, 71.0, 73.0, 75.0, 77.0, 79.0, 81.0, 83.0, 85.0, 87.0, 89.0, 91.0, 93.0, 95.0, 97.0, 99.0, 101.0, 103.0, 105.0, 107.0, 109.0, 111.0, 113.0, 115.0, 117.0, 119.0, 121.0, 123.0, 125.0, 127.0, 129.0, 131.0, 133.0, 135.0, 137.0, 139.0, 141.0, 143.0, 145.0, 147.0, 149.0, 151.0, 153.0, 155.0, 157.0, 159.0]
y = [45.6501, 37.4855, 40.4035, 51.4948, 55.8648, 48.9723, 60.4494, 42.7136, 20.6929, 36.7847, 44.4601, 54.04, 52.4895, 45.1991, 46.8203, 44.5827, 65.8803, 53.5398, 69.5158, 46.5372, 37.1557, 43.9031, 39.9325, 35.5248, 34.3531, 57.8377, 37.9208, 26.6508, 27.2333, 49.3798, 47.8627, 54.2795, 50.0892, 40.9849, 37.4014, 300.7947, 299.4254, 288.5113, 313.2906, 319.0095, 291.0726, 308.075, 298.451, 311.1485, 320.4832, 303.9229, 310.4584, 325.6287, 307.7328, 301.5581, 308.7813, 308.6791, 305.1343, 307.5148, 334.6374, 310.476, 315.6943, 326.0586, 298.6766, 305.6225]
最小工作示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y, linewidth = 1.2, label = 'd3')
ax.set_yticks([t for t in range(0,390,30)])
ax.set_xticks([t for t in range(50,200,50)])
ax.legend(loc='lower right',prop={'size': 14})
plt.show()
使用基本 Python,如您的列表所示,而不是像 numpy
这样的高级库,您可以使用基本函数将绘图的两个部分分开。但是,考虑到您的具体问题,您可能更喜欢极坐标图:
from matplotlib import pyplot as plt
#two subplots with two different approaches
fig, (ax1, ax2) = plt.subplots(2, figsize=(5, 10))
#first approach - separating the list at the jump point
ymin = 0
ymax = 360
#pseudo-threshold calculation, just the first index in your list with a value above the threshold
threshold = 100
breakpoint = next(a[0] for a in enumerate(y) if a[1] > threshold)
#separating the lists at this breakpoint, creating intermediate point
y1 = y[:breakpoint] + [ymin]
y2 = [ymax] + y[breakpoint:]
x12 = 0.5 * (x[breakpoint-1] + x[breakpoint])
x1 = x[:breakpoint] + [x12]
x2 = [x12] + x[breakpoint:]
#plotting of the upper subplot
ax1.plot(x1, y1, c="r", label="jump")
ax1.plot(x2, y2, c="r")
ax1.legend()
ax1.set_ylim(ymin, ymax)
#second approach - a polar plot
#convert deg into rad, here with numpy
import numpy as np
angle = np.deg2rad(y)
#plot the second subplot using polar coordinates
ax2 = plt.subplot(212, projection='polar')
ax2.plot(angle, x, c="r", label = "same jump")
#making it look nicer with clockwise rotation and 0 degree at the top
ax2.set_theta_direction(-1)
ax2.set_theta_zero_location('N')
ax2.set_rlabel_position(180)
ax2.set_ylim(0.9 * x[0], 1.1 * x[-1])
ax2.legend(loc=(-0.07,0.97))
plt.show()
这为您提供了两个视图以供比较:
我一直在思考另一个问题——如何检测转换。通常,信号的峰值检测很困难,但在您的情况下,条件相当简单:如果两个数据点之间的变化超过 180°,那么您应该在边界上绘制它。现在,我的建议是在循环中以一种相当尴尬的方式使用 numpy
。 I have been asking for suggestions of how to improve the segment extraction,唉,没有启发性的答案出来。至少代码有效。
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots(figsize=(10, 5))
ymin = 0
ymax = 360
colour = "blue"
xarr, yarr = np.asarray([x, y])
#create index array for all points with jumps by more than 180 degrees
ind = list(np.where(np.abs(np.diff(yarr, append=yarr[-1])) > np.mean([ymin, ymax])))[0]
#if ind is not empty extract segment
if ind.size:
#found breakpoints
for i, j in enumerate(ind):
#first segment
if i==0:
#first trace
xcurr = np.copy(xarr[:j+2])
ycurr = np.copy(yarr[:j+2])
xcurr[-1] = np.mean(xcurr[-2:])
ycurr[-1] = (ymin, ymax)[ycurr[-1]<np.mean([ymin, ymax])]
#all following segments
else:
xcurr = np.copy(xarr[ind[i-1]:j+2])
ycurr = np.copy(yarr[ind[i-1]:j+2])
xcurr[0] = np.mean(xcurr[:2])
ycurr[0] = (ymin, ymax)[ycurr[0]<np.mean([ymin, ymax])]
xcurr[-1] = np.mean(xcurr[-2:])
ycurr[-1] = (ymin, ymax)[ycurr[-1]<np.mean([ymin, ymax])]
plt.plot(xcurr, ycurr, c=colour)
#last segment
xcurr = np.copy(xarr[j:])
ycurr = np.copy(yarr[j:])
xcurr[0] = np.mean(xcurr[:2])
ycurr[0] = (ymin, ymax)[ycurr[0]<np.mean([ymin, ymax])]
else:
#ind was empty - plot entire array
xcurr = np.copy(xarr)
ycurr = np.copy(yarr)
plt.plot(xcurr, ycurr, c=colour)
plt.ylim(ymin, ymax)
plt.yticks(np.linspace(ymin, ymax, 13))
plt.show()
带有以下多次跳转测试数据的示例输出
测试数据:
x = [41.0, 43.0, 45.0, 47.0, 49.0, 51.0, 53.0, 55.0, 57.0, 59.0, 61.0, 63.0, 65.0, 67.0, 69.0, 71.0, 73.0, 75.0, 77.0, 79.0, 81.0, 83.0, 85.0, 87.0, 89.0, 91.0, 93.0, 95.0, 97.0, 99.0, 101.0, 103.0, 105.0, 107.0, 109.0, 111.0, 113.0, 115.0, 117.0, 119.0, 121.0, 123.0, 125.0, 127.0, 129.0, 131.0, 133.0, 135.0, 137.0, 139.0, 141.0, 143.0, 145.0, 147.0, 149.0, 151.0, 153.0, 155.0, 157.0, 159.0]
y = [45.6501, 37.4855, 40.4035, 31.4948, 155.8648, 148.9723, 180.4494, 142.7136, 220.6929, 236.7847, 244.4601, 254.04, 252.4895, 245.1991, 246.8203, 244.5827, 265.8803, 253.5398, 269.5158, 246.5372, 237.1557, 243.9031, 239.9325, 235.5248, 234.3531, 257.8377, 37.9208, 26.6508, 27.2333, 349.3798, 347.8627, 354.2795, 350.0892, 340.9849, 337.4014, 300.7947, 99.4254, 88.5113, 13.2906, 19.0095, 191.0726, 208.075, 198.451, 111.1485, 120.4832, 103.9229, 110.4584, 125.6287, 107.7328, 101.5581, 108.7813, 108.6791, 105.1343, 107.5148, 134.6374, 110.476, 15.6943, 26.0586, 98.6766, 105.6225]
所以,我有一张键的二面角图。 y 轴仅从 0-360,x 轴是帧(想想时间步长)。如果值超过 360,我需要绘图“循环”回零,并绘制两点之间的最短距离(如果需要越过图形的边缘并“循环”回而不是穿过图形).
d3的图看起来还可以,但实际上需要跳过图的边缘而不是越过图的边缘。
d5 的情节有一个严重的问题,对于一个小的旋转有一个巨大的跳跃只是因为它恰好刚好低于 0 度。
我希望这两个图都向底部绘制(向零)并重新出现在图的顶部,有效地选择数据点之间的最短距离。我不希望涉及图的翻译的解决方案来删除这些人工制品(它有效,我已经做到了,但你失去了关于角度真实值的信息)。可以绘制“零以下”(因此 y 轴从 300 到 360|0 到 200 到 300)的解决方案也很棒。使用其他库的解决方案非常好。如果需要我可以提供数据集。
我希望它执行的操作示例(绿线)
我曾尝试寻找类似的解决方案,但无济于事。关于周期性边界的问题使用 numpy 数据集掩码来隐藏某些跳跃,但它们具有连续函数(我的是“跳跃”)。
感谢您的帮助,我将不胜感激。
数据集(使它们比图表上的小一点,只保留跳过):
D3:
x = [41.0, 43.0, 45.0, 47.0, 49.0, 51.0, 53.0, 55.0, 57.0, 59.0, 61.0, 63.0, 65.0, 67.0, 69.0, 71.0, 73.0, 75.0, 77.0, 79.0, 81.0, 83.0, 85.0, 87.0, 89.0, 91.0, 93.0, 95.0, 97.0, 99.0, 101.0, 103.0, 105.0, 107.0, 109.0, 111.0, 113.0, 115.0, 117.0, 119.0, 121.0, 123.0, 125.0, 127.0, 129.0, 131.0, 133.0, 135.0, 137.0, 139.0, 141.0, 143.0, 145.0, 147.0, 149.0, 151.0, 153.0, 155.0, 157.0, 159.0]
y = [45.6501, 37.4855, 40.4035, 51.4948, 55.8648, 48.9723, 60.4494, 42.7136, 20.6929, 36.7847, 44.4601, 54.04, 52.4895, 45.1991, 46.8203, 44.5827, 65.8803, 53.5398, 69.5158, 46.5372, 37.1557, 43.9031, 39.9325, 35.5248, 34.3531, 57.8377, 37.9208, 26.6508, 27.2333, 49.3798, 47.8627, 54.2795, 50.0892, 40.9849, 37.4014, 300.7947, 299.4254, 288.5113, 313.2906, 319.0095, 291.0726, 308.075, 298.451, 311.1485, 320.4832, 303.9229, 310.4584, 325.6287, 307.7328, 301.5581, 308.7813, 308.6791, 305.1343, 307.5148, 334.6374, 310.476, 315.6943, 326.0586, 298.6766, 305.6225]
最小工作示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y, linewidth = 1.2, label = 'd3')
ax.set_yticks([t for t in range(0,390,30)])
ax.set_xticks([t for t in range(50,200,50)])
ax.legend(loc='lower right',prop={'size': 14})
plt.show()
使用基本 Python,如您的列表所示,而不是像 numpy
这样的高级库,您可以使用基本函数将绘图的两个部分分开。但是,考虑到您的具体问题,您可能更喜欢极坐标图:
from matplotlib import pyplot as plt
#two subplots with two different approaches
fig, (ax1, ax2) = plt.subplots(2, figsize=(5, 10))
#first approach - separating the list at the jump point
ymin = 0
ymax = 360
#pseudo-threshold calculation, just the first index in your list with a value above the threshold
threshold = 100
breakpoint = next(a[0] for a in enumerate(y) if a[1] > threshold)
#separating the lists at this breakpoint, creating intermediate point
y1 = y[:breakpoint] + [ymin]
y2 = [ymax] + y[breakpoint:]
x12 = 0.5 * (x[breakpoint-1] + x[breakpoint])
x1 = x[:breakpoint] + [x12]
x2 = [x12] + x[breakpoint:]
#plotting of the upper subplot
ax1.plot(x1, y1, c="r", label="jump")
ax1.plot(x2, y2, c="r")
ax1.legend()
ax1.set_ylim(ymin, ymax)
#second approach - a polar plot
#convert deg into rad, here with numpy
import numpy as np
angle = np.deg2rad(y)
#plot the second subplot using polar coordinates
ax2 = plt.subplot(212, projection='polar')
ax2.plot(angle, x, c="r", label = "same jump")
#making it look nicer with clockwise rotation and 0 degree at the top
ax2.set_theta_direction(-1)
ax2.set_theta_zero_location('N')
ax2.set_rlabel_position(180)
ax2.set_ylim(0.9 * x[0], 1.1 * x[-1])
ax2.legend(loc=(-0.07,0.97))
plt.show()
这为您提供了两个视图以供比较:
我一直在思考另一个问题——如何检测转换。通常,信号的峰值检测很困难,但在您的情况下,条件相当简单:如果两个数据点之间的变化超过 180°,那么您应该在边界上绘制它。现在,我的建议是在循环中以一种相当尴尬的方式使用 numpy
。 I have been asking for suggestions of how to improve the segment extraction,唉,没有启发性的答案出来。至少代码有效。
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots(figsize=(10, 5))
ymin = 0
ymax = 360
colour = "blue"
xarr, yarr = np.asarray([x, y])
#create index array for all points with jumps by more than 180 degrees
ind = list(np.where(np.abs(np.diff(yarr, append=yarr[-1])) > np.mean([ymin, ymax])))[0]
#if ind is not empty extract segment
if ind.size:
#found breakpoints
for i, j in enumerate(ind):
#first segment
if i==0:
#first trace
xcurr = np.copy(xarr[:j+2])
ycurr = np.copy(yarr[:j+2])
xcurr[-1] = np.mean(xcurr[-2:])
ycurr[-1] = (ymin, ymax)[ycurr[-1]<np.mean([ymin, ymax])]
#all following segments
else:
xcurr = np.copy(xarr[ind[i-1]:j+2])
ycurr = np.copy(yarr[ind[i-1]:j+2])
xcurr[0] = np.mean(xcurr[:2])
ycurr[0] = (ymin, ymax)[ycurr[0]<np.mean([ymin, ymax])]
xcurr[-1] = np.mean(xcurr[-2:])
ycurr[-1] = (ymin, ymax)[ycurr[-1]<np.mean([ymin, ymax])]
plt.plot(xcurr, ycurr, c=colour)
#last segment
xcurr = np.copy(xarr[j:])
ycurr = np.copy(yarr[j:])
xcurr[0] = np.mean(xcurr[:2])
ycurr[0] = (ymin, ymax)[ycurr[0]<np.mean([ymin, ymax])]
else:
#ind was empty - plot entire array
xcurr = np.copy(xarr)
ycurr = np.copy(yarr)
plt.plot(xcurr, ycurr, c=colour)
plt.ylim(ymin, ymax)
plt.yticks(np.linspace(ymin, ymax, 13))
plt.show()
带有以下多次跳转测试数据的示例输出
测试数据:
x = [41.0, 43.0, 45.0, 47.0, 49.0, 51.0, 53.0, 55.0, 57.0, 59.0, 61.0, 63.0, 65.0, 67.0, 69.0, 71.0, 73.0, 75.0, 77.0, 79.0, 81.0, 83.0, 85.0, 87.0, 89.0, 91.0, 93.0, 95.0, 97.0, 99.0, 101.0, 103.0, 105.0, 107.0, 109.0, 111.0, 113.0, 115.0, 117.0, 119.0, 121.0, 123.0, 125.0, 127.0, 129.0, 131.0, 133.0, 135.0, 137.0, 139.0, 141.0, 143.0, 145.0, 147.0, 149.0, 151.0, 153.0, 155.0, 157.0, 159.0]
y = [45.6501, 37.4855, 40.4035, 31.4948, 155.8648, 148.9723, 180.4494, 142.7136, 220.6929, 236.7847, 244.4601, 254.04, 252.4895, 245.1991, 246.8203, 244.5827, 265.8803, 253.5398, 269.5158, 246.5372, 237.1557, 243.9031, 239.9325, 235.5248, 234.3531, 257.8377, 37.9208, 26.6508, 27.2333, 349.3798, 347.8627, 354.2795, 350.0892, 340.9849, 337.4014, 300.7947, 99.4254, 88.5113, 13.2906, 19.0095, 191.0726, 208.075, 198.451, 111.1485, 120.4832, 103.9229, 110.4584, 125.6287, 107.7328, 101.5581, 108.7813, 108.6791, 105.1343, 107.5148, 134.6374, 110.476, 15.6943, 26.0586, 98.6766, 105.6225]