Griff
Griff

Reputation: 2124

adjust color, size and type of marker after data has been plotted in matplotlib

I've plotted a bunch of scatter points then redraw the canvas:

self.display_points = ax.scatter(x,y)
wx.CallAfter(self.display.canvas.draw)

I have an object which contains the color. If this is changed by the user from the GUI, I want to be able to change the color of the points without having to replot the data.

def _color_changed(self):
    if hasattr(self, '_display_points'): 
        self._display_points.set_facecolors(self.color)
        wx.CallAfter(self.display.canvas.draw)

how is this done for the size of the marker and the type of marker... ie. what should X be in _display_points.set_X to change each of the plotted components. Is there somewhere these attributes can be found? Thanks.

Upvotes: 4

Views: 2351

Answers (1)

tacaswell
tacaswell

Reputation: 87416

scatter returns a PathCollection object, which you can see has a relatively limited api for setting things after the fact. The Collection family of classes trades the ability to update later for more efficient drawing.

If you are not using scatter's ability to set the size and color of each point separately you are much better off using

self.display_points, = ax.plot(x, y, marker='o', linestyle='none')

which will give you a Line2D object back and look identical your scatter plot. Line2D has a much more flexible api which includes set_marker and set_markersize.

Upvotes: 3

Related Questions