Python + GNU Plot: handling missing values

For clarity, I highlighted my problem and used a small but complete snippet to describe it.

I have a bunch of data, but there are a lot of missing parts. I want to ignore them (splitting on a graph if it is a line graph). I have installed "?" to indicate missing data. Here is my snippet:

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")

But this does not work:

> 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.

What will go wrong?

+3
source share
1 answer

Gnuplot calls numpy.asarrayto convert your Python list to a numpy array. Unfortunately, this command (c dtype=numpy.float32) is not compatible with a Python list containing strings.

You can reproduce the error as follows:

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.

In addition, the python Gnuplot module (version 1.7) reports

  • ( gnuplot "set missing" ).

, 1.8.

gnuplot? matplotlib?

+4

Source: https://habr.com/ru/post/1789142/


All Articles