编程语言
首页 > 编程语言> > Python GNU绘图:处理缺失值

Python GNU绘图:处理缺失值

作者:互联网

为了清楚起见,我隔离了我的问题,并使用了一个很小但完整的代码段来描述它.

我有一堆数据,但是有很多缺失的部分.我想忽略这些(如果是折线图,则在图形中会出现中断).我已经设定 ”?”成为丢失数据的符号.这是我的片段:

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.

怎么了

解决方法:

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 module (version 1.7) docs

  • There is no provision for missing data points in array data (which
    gnuplot allows via the ‘set missing’ command).

我不确定在1.8版中是否已解决此问题.

你和gnuplot结婚了吗? Have you tried matplotlib

标签:gnuplot,python
来源: https://codeday.me/bug/20191105/1995312.html