How to update matplotlib hexbin graph?

I have a matplotlib hexbinbuilt in GTK.Windowwhich displays some data (x, y). I want to be plotupdated when new data is received (via UDP). However, I have a problem.

I can make it work in different ways, but none of them were "effective" (meaning - redrawing plottakes too much time). I looked here and tried to simulate my hexbin after the suggested answer, but could not get this to work at all. I keep getting the following error:

TypeError: 'PolyCollection' object is not iterable.

I assume that hexbinsit cannot be updated in the same way as the standard one plots.

Code example:

class graph:
    def __init__(self):
        self.window = gtk.Window()
        self.figure = plt.figure()
        self.ax = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.figure)
        self.window.add(self.canvas)

        self.graph = None

    def plot(self, xData, yData):
        if len(xData) > 1 and len(yData) > 1:
            self.graph, = self.ax.hexbin(self.xData, self.yData) 
            ###############################################
            ####This is where the code throws the error####

    def update(self, xData, yData):
        self.graph.set_xdata(np.append(self.graph.get_xdata(), xData))
        self.graph.set_ydata(np.append(self.graph.get_ydata(), yData))
        self.figure.canvas.draw()

The code is used as follows:

graph = graph()
graph.plot(someXData, someYData)
# when new data is received
graph.update(newXData, newYData)

, . matplotlib, , . ( , )

, : matplotlib hexbin?


: ',' self.graph = self.ax.hexbin(...)

: AttributeError: 'PolyCollection' object has no attribute 'set_xdata'

+5
3

, , hexbin x, y → . polyCollection - , , , - , .

- hexbin .

2d ( imshow), hexbin, ( polyCollection) .

+2

:

 self.graph, = self.ax.hexbin(self.xData, self.yData)

( , ) - , , self.ax.hexbin .

 self.graph = self.ax.hexbin(self.xData, self.yData)

TypeError exception. - .

+2

, set_xdata - update_from. , ,

def update(self, xData, yData):
    # update your data structures
    self.xData = np.append(self.xData, xData)
    self.yData = np.append(self.yData, yData)

    # create a new hexbin - not connected to anything, with the new data
    new_hexbin = self.ax.hexbin(self.xData, self.yData)

    # update the viewed hexbin from the new one
    self.graph.update_from(new_hexbin)
    self.figure.canvas.draw()

Remember that without additional code or explanation, it really is just a guess at work! The documentation for this class is here and the update_frommethod is from the parent class .

0
source

All Articles