Python 为什么matplotlib会推断/绘制缺失值?

Python 为什么matplotlib会推断/绘制缺失值?,python,matplotlib,plot,Python,Matplotlib,Plot,我有一种情况,有时,一系列的数据是不可用的。我正在实时绘制来自传感器的值,这些值可以通过用户交互打开和关闭,因此我不能确定这些值始终是一系列的。用户可以启动传感器,然后将其关闭或再次打开,但在这种情况下,matplotlib会从最后一个端点和新的起点绘制一条线 我绘制的数据如下: [[ 5. 22.57011604] [ 6. 22.57408142] [ 7. 22.56350136] [ 8. 22.563

我有一种情况,有时,一系列的数据是不可用的。我正在实时绘制来自传感器的值,这些值可以通过用户交互打开和关闭,因此我不能确定这些值始终是一系列的。用户可以启动传感器,然后将其关闭或再次打开,但在这种情况下,matplotlib会从最后一个端点和新的起点绘制一条线

我绘制的数据如下:

[[  5.          22.57011604]
 [  6.          22.57408142]
 [  7.          22.56350136]
 [  8.          22.56394005]
 [  9.          22.56790352]
 [ 10.          22.56451225]
 [ 11.          22.56481743]
 [ 12.          22.55789757]
  #Missing x vals. Still plots straight line..
 [ 29.          22.55654716]
 [ 29.          22.56066513]
 [ 30.          22.56110382]
 [ 31.          22.55050468]
 [ 32.          22.56550789]
 [ 33.          22.56213379]
 [ 34.          22.5588932 ]
 [ 35.          22.54829407]
 [ 35.          22.56697655]
 [ 36.          22.56005478]
 [ 37.          22.5568161 ]
 [ 38.          22.54621696]
 [ 39.          22.55033493]
 [ 40.          22.55079269]
 [ 41.          22.55475616]
 [ 41.          22.54783821]
 [ 42.          22.55195618]]
我的绘图函数看起来非常简单,如下所示:

def plot(self, data)
    for name, xy_dict in data.iteritems():
        x_vals = xy_dict['x_values']
        y_vals = xy_dict['y_values']
        line_to_plot = xy_dict['line_number']
        self.lines[line_to_plot].set_xdata(x_vals)
        self.lines[line_to_plot].set_ydata(y_vals)

有人知道为什么会这样吗?在绘图时,我是否必须考虑非序列x和y值?看来matplotlib应该自己解决这个问题。。否则,我必须将列表拆分为较小的列表并绘制这些列表?

Matplotlib将用线连接所有消耗数据点


如果要避免这种情况,可以在缺少的x值处拆分数据,并分别绘制两个拆分的列表。

一个选项是在缺少数据的位置添加虚拟项(在您的情况下,显然是当
x
更改超过1时),并将其设置为屏蔽元素。这样,matplotlib将跳过线段。例如:

import numpy as np
import matplotlib.pylab as pl

# Your data, with some additional elements deleted...
data = np.array(
[[  5., 22.57011604],
 [  6., 22.57408142],
 [  9., 22.56790352],
 [ 10., 22.56451225],
 [ 11., 22.56481743],
 [ 12., 22.55789757],
 [ 29., 22.55654716],
 [ 33., 22.56213379],
 [ 34., 22.5588932 ],
 [ 35., 22.54829407],
 [ 40., 22.55079269],
 [ 41., 22.55475616],
 [ 41., 22.54783821],
 [ 42., 22.55195618]])

x = data[:,0]
y = data[:,1]

# Difference from element to element in x
dx = x[1:]-x[:-1]

# Wherever dx > 1, insert a dummy item equal to -1
x2 = np.insert(x, np.where(dx>1)[0]+1, -1)
y2 = np.insert(y, np.where(dx>1)[0]+1, -1)

# As discussed in the comments, another option is to use e.g.:
#x2 = np.insert(x, np.where(dx>1)[0]+1, np.nan)
#y2 = np.insert(y, np.where(dx>1)[0]+1, np.nan)
# and skip the masking step below.

# Mask elements which are -1
x2 = np.ma.masked_where(x2 == -1, x2)
y2 = np.ma.masked_where(y2 == -1, y2)

pl.figure()
pl.subplot(121)
pl.plot(x,y)
pl.subplot(122)
pl.plot(x2,y2)

另一个选项是将
None
numpy.nan
作为y的值

例如,这显示了一条断开的线路:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[5,6,None,7,8])

我仍然可以像以前一样设置x和y数据,还是需要在新行上绘制它们?(这不是很好,因为它们会改变颜色等)。如果数据点在同一条matplotlib线上,我会更喜欢。真不错!谢谢你的提示。我目前正在考虑使用np.nan来中断一行,这显然是可行的。我将首先尝试,稍后再尝试。掩蔽似乎相对较慢,例如,在带有随机数的数组上,
np.ma.masked_,其中(xI无法重现,但给出以下答案:
x2
数组似乎被解释为带有整数的数组。您可以尝试类似于
x2.astype(np.float64)的方法
是的,我的解决方案需要
numpy
数组,因此如果从Python列表开始,则需要首先执行强制转换(在这里可以传递
dtype
关键字,例如
x=np.array(x,dtype=np.float64)
)啊,真的很好。试过了,效果也不错。非常感谢你的回答!我也发现了这一点。我目前正在尝试将@Bart的答案合并到np.nan中,但出现了一些错误。谢谢!