2

我想在笛卡尔图上绘制风向测量值,X 轴为时间,Y 轴为方向。因为从 359 度移动到 0 度时方向会发生回绕,所以画一条连接 359 到 0 的线是不合适的。

如果较短的跳跃没有包裹,是否可以有条件地绘制连接线?即这里是一个值序列,在适当的地方有连接线:

10—15—30—90—150—290—350 40—50—20 310—250—150

我假设我会使用类似下面的公式来确定 A 和 B 之间是否应该有一条线:

max(A,B) - min(A,B) <= 180
4

2 回答 2

3

While writing the question, I came up with a working approach: Simply adding a None value indicates that the data are not continuous and the line is broken.

To make it perfect, I added points outside my range so that lines will eg leave the plot at the top and come out of the bottom.

This was done with a simple generator that adds the relevant data points:

def break_degree_wrap(values):
    values1, values2 = itertools.tee(values)

    # Yield the first value
    yield next(values2)

    for (prev_datetime, prev_val), (datetime, val) in itertools.izip(values1, values2):

        # If the data wraps over the top
        if val > prev_val and val - prev_val > 180:
            yield (datetime, val - 360)
            yield (datetime, None)
            yield (prev_datetime, prev_val + 360)

        # If the data wraps under the bottom
        elif val < prev_val and prev_val - val > 180:
            yield (datetime, val + 360)
            yield (datetime, None)
            yield (prev_datetime, prev_val - 360)

        # Add each original value
        yield (datetime, val)

This could be easily generalised to accept a valid range, instead of the hardcoded (0, 360).

Here is how it looks (excuse the poor axes :-) Wind direction

于 2013-04-17T10:51:57.240 回答
3

我总是使用 np.diff() 函数来获取变化大于 180 度的点并插入一个蒙面元素。主要的缺点是您还必须为绘图保留一个“索引”。

所以:

a = np.array([10,15,30,90,150,290,350,40,50,20,310,250,150])
idx = np.arange(len(a))

b = np.diff(a)
mask = np.where(np.abs(b) >= 180)[0]+1

c = np.ma.masked_equal(np.insert(a, mask, -1), -1)
idx = np.ma.masked_equal(np.insert(idx, mask, -1), -1)

fig, ax = plt.subplots(figsize=(10,3))

ax.plot(idx, c)
ax.set_xticks(idx)
ax.set_ylim(0,360)

在此处输入图像描述

这不会插入超出范围的值,但我想可以添加。您可以插入 2 个元素再次“出”和“入”图表,而不是 1 个蒙面元素。

另请注意,180 度的阈值只是一个假设,除非您实际测量它,否则您并不真正知道风是在倒退还是转向。

于 2013-04-17T12:21:36.660 回答