Plotting elements of a class

108 views
Skip to first unread message

andrea gotelli

unread,
Jun 19, 2020, 1:43:55 PM6/19/20
to pyqtgraph
 Hi,


I have this problem where i need to plot elements that are stored into an list containing a UDT.

the class is the following:

class Measurement:
        def __init__(self, time, mh_dist, color, x, y):
            self.time = float(time)
            self.mh_dist = float(mh_dist)
            self.color = color
            self.x = float( x )
            self.y = float( y )

The I create a list containing elements of this class.

processedMeasurements = []

for topic, meas, t in bag.read_messages(topics=['/Measurements']):

        if meas.accepted:

            processedMeasurements.append( Measurement(  t.to_sec(),
                                                        meas.distance,
                                                        'b',
                                                        meas.pose.position.x,
                                                        meas.pose.position.y) )
        else:
            processedMeasurements.append( Measurement(  t.to_sec(),
                                                        meas.distance,
                                                        'r',
                                                        meas.pose.position.x,
                                                        meas.pose.position.y) )

The only thing that is important to understan here is that I am saving data readed from a file. Later I would like to plot this data.

For example, the measurements contains a x and y position. I would like to plot this position for every element in the list processedMeasurements.

However, doing something like:

for measurement in processedMeasurements:
        p1.plot(measurement.x, measurement.y, pen=None, symbol='o')

Gives me the following error:

Traceback (most recent call last):
  File "/home/agotelli/catkin_ws/src/Project1/plotting/scripts/plotting", line 514, in <module>
    p1.plot(measurement.x, measurement.y, pen=None, symbol='o')
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/graphicsItems/PlotItem/PlotItem.py", line 640, in plot
    item = PlotDataItem(*args, **kargs)
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/graphicsItems/PlotDataItem.py", line 185, in __init__
    self.setData(*args, **kargs)
  File "/usr/local/lib/python2.7/dist-packages/pyqtgraph/graphicsItems/PlotDataItem.py", line 395, in setData
    raise Exception('When passing two unnamed arguments, both must be a list or array of values. (got %s, %s)' % (str(type(args[0])), str(type(args[1]))))
Exception: When passing two unnamed arguments, both must be a list or array of values. (got <type 'float'>, <type 'float'>)


Does anybody knows how to solve this problem??

Thank you in advance.

Kenneth Lyons

unread,
Jun 20, 2020, 1:32:28 AM6/20/20
to pyqtgraph
The plot method expects multiple points in lists or arrays, and you're passing scalars for the x and y arguments. One thing you could do is extract the coordinates from all measurements into lists and pass them to plot. For example:

xlist = [m.x for m in processedMeasurements]
ylist
= [m.y for m in processedMeasurements]
p1
.plot(xlist, ylist, pen=None, symbol='o')

Is there a way to rewrite the exception message that could make it more clear?

By the way, you can plot a single point at a time by wrapping the coordinate values in lists, e.g. plot([0.], [1.]), but this approach won't scale well as each plot call generates a separate PlotDataItem.

andrea gotelli

unread,
Jun 20, 2020, 2:40:52 AM6/20/20
to pyqt...@googlegroups.com
Thank you very much for the reply.

I have tried the second method yesterday, but for creating a lots of plots, I also experienced lag..

Now I have another question that should be the final.


Now plotting all these points, I would like to have two groups, one group with normal blue points, and a second group with small red points.

The selection is based on the Boolean in the processedMeasurements. For example:

for measurement in processedMeasurements:,
     if measurement.accepted == True:
          # Then put in group one...


Is there a way to pass the color and the symbol size as array??

Thank you in advance for your time.

Best regards,
Andrea Gotelli.


From: pyqt...@googlegroups.com <pyqt...@googlegroups.com> on behalf of Kenneth Lyons <ixjl...@gmail.com>
Sent: Saturday, June 20, 2020 7:32:28 AM
To: pyqtgraph <pyqt...@googlegroups.com>
Subject: [pyqtgraph] Re: Plotting elements of a class
 
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/db3cf471-8c19-431c-8fe9-6e85bb907c8fo%40googlegroups.com.

Kenneth Lyons

unread,
Jun 20, 2020, 3:04:39 AM6/20/20
to pyqtgraph
Yes, the symbolSize and symbolBrush arguments accept lists, for example:

red = pg.mkBrush('r')
blue
= pg.mkBrush('b')
brushes
= [blue if m.accept else red for m in measurements]
sizes
= [10 if m.accept else 5 for m in measurements]

p1
.plot(xlist, ylist, pen=None, symbolSize=sizes, symbolBrush=brushes)

To unsubscribe from this group and stop receiving emails from it, send an email to pyqt...@googlegroups.com.

andrea gotelli

unread,
Jun 20, 2020, 3:14:30 AM6/20/20
to pyqt...@googlegroups.com
Thank you very much for the help.

Have a nice day (hoping for you is morning as for me)

Thank you in advance for your time.

Best regards,
Andrea Gotelli.


Sent: Saturday, June 20, 2020 9:04:38 AM
To: pyqtgraph <pyqt...@googlegroups.com>
Subject: Re: [pyqtgraph] Re: Plotting elements of a class
 
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/7d8b055e-098a-4de7-82d8-efd9fddaef2co%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages