Python + GNU Plot:处理缺失值

3
为了更清晰地阐述问题,我已经将问题隔离出来,并使用了一个小但完整的片段来描述它。
我有许多数据,但缺失了很多部分。我想忽略这些(如果它是折线图,则为图形中断)。我已将“?”设置为缺少数据的符号。以下是我的片段:
import math
import Gnuplot

gp = Gnuplot.Gnuplot(persist=1)
gp("set datafile missing '?'")

x = range(1000)

y = [math.sin(a) + math.cos(a) + math.tan(a) for a in x]

# Force a piece of missing data
y[4] = '?'

data = Gnuplot.Data(x, y, title='Plotting from Python')
gp.plot(data);

gp.hardcopy(filename="pyplot.png",terminal="png")

但是它不起作用:

> python missing_test.py
Traceback (most recent call last):
  File "missing_test.py", line 8, in <module>
    data = Gnuplot.Data(x, y, title='Plotting from Python')
  File "/usr/lib/python2.6/dist-packages/Gnuplot/PlotItems.py", line 560, in Data
    data = utils.float_array(data)
  File "/usr/lib/python2.6/dist-packages/Gnuplot/utils.py", line 33, in float_array
    return numpy.asarray(m, numpy.float32)
  File "/usr/lib/python2.6/dist-packages/numpy/core/numeric.py", line 230, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

出了什么问题?

1个回答

4
Gnuplot正在调用numpy.asarray将您的Python列表转换为numpy数组。不幸的是,这个命令(带有dtype=numpy.float32)与包含字符串的Python列表不兼容。您可以像这样重现错误:
In [36]: np.asarray(['?',1.0,2.0],np.float32)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/usr/lib/python2.6/dist-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
    228 
    229     """
--> 230     return array(a, dtype, copy=False, order=order)
    231 
    232 def asanyarray(a, dtype=None, order=None):

ValueError: setting an array element with a sequence.

此外,Gnuplot python模块(版本1.7)文档中提到:

  • 数组数据中没有缺失数据点的处理方式(gnuplot通过“set missing”命令允许缺失数据点)。

我不确定这个问题在1.8版本中是否已经解决。

你对gnuplot有多忠诚?你尝试过matplotlib吗


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接