在线条图中放置间隙/中断 [英] Put a gap/break in a line plot

查看:54
本文介绍了在线条图中放置间隙/中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含有效的连续"传感器读数,偶尔会有间隙.

I have a data set with effectively "continuous" sensor readings, with the occasional gap.

但是有几个时期没有记录数据.这些差距明显长于样本期.

However there are several periods in which no data was recorded. These gaps are significantly longer than the sample period.

默认情况下,pyplot将每个数据点连接到下一个数据点(如果我设置了线型),但是当我将两个数据点连接在长间隙的任一侧时,这会引起误解.

By default, pyplot connects each data point to the next (if I have a line style set), however I feel that this is slightly misleading when it connects the two data points either side of a long gap.

我宁愿那里没有线路;也就是说,我希望行在出现间隙后停止并重新开始.

I would prefer to simply have no line there; that is, I would like the line to stop and to start again after the gap.

我尝试在这些间隙部分中使用y值 None 添加一个元素,但似乎将线发送回绘图的早期部分(尽管奇怪的是,这些线并没有出现在所有缩放级别).

I have tried adding in an element in these gap sections with the y-value None, but seems to send the line back to an earlier part of the plot (though strangely these lines don't appear at all zoom levels).

我想到的另一个选择是通过单独调用 plot 来简单地绘制每个部分,但这会有点丑陋和麻烦.

The other option I have thought of is to simply plot each piece with a separate call to plot, but this would be a bit ugly and cumbersome.

是否有更优雅的方法来实现这一目标?

Is there a more elegant way of achieving this?

下面是一个演示该行为的最小工作示例.第一个情节是我要避免的连接线.第二个图显示添加一个 None 值似乎有效,但是如果您平移图的视图,您会得到第三个图中所示的内容,一条线跳转到图的较早部分.

Below is a minimal working example demonstrating the behaviour. The first plot is the joining line I am trying to avoid. The second plot shows that adding a None value appears to work, however if you pan the view of the plot, you get what is shown in the third figure, a line jumping to an earlier part of the plot.

import numpy as np
import matplotlib.pyplot as plt

t1 = np.arange(0, 8, 0.05)
t2 = np.arange(10, 14, 0.05)
t = np.concatenate([t1, t2])
c = np.cos(t)

fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.set_title('Undesirable joining line')


t1 = np.arange(0, 8, 0.05)
t2 = np.arange(10, 14, 0.05)
c1 = np.cos(t1)
c2 = np.cos(t2)
t = np.concatenate([t1, t1[-1:], t2])
c = np.concatenate([c1, [None,], c2])

fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.set_title('Ok if you don\'t pan the plot')

fig = plt.figure()
ax = fig.gca()
ax.plot(t, c)
ax.axis([-1, 12, -0.5, 1.25])
ax.set_title('Strange jumping line')

plt.show()

推荐答案

掩码数组非常适用于此.您只需要屏蔽您不想连接的第一个点:

Masked arrays work well for this. You just need to mask the first of the points you don't want to connect:

import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt

t1 = np.arange(0, 8, 0.05)
mask_start = len(t1)
t2 = np.arange(10, 14, 0.05)
t = np.concatenate([t1, t2])
c = np.cos(t)     # an aside, but it's better to use numpy ufuncs than list comps

mc = ma.array(c)
mc[mask_start] = ma.masked
plt.figure()
plt.plot(t, mc)
plt.title('Using masked arrays')

plt.show()

至少在我的系统(OSX、Python 2.7、mpl 1.1.0)上,我在平移等方面没有任何问题.

At least on my system (OSX, Python 2.7, mpl 1.1.0), I don't have any issues with panning, etc.

这篇关于在线条图中放置间隙/中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆