How to change the plot color after a button press in matplotlib/tkinter? How to change the plot color after a button press in matplotlib/tkinter? tkinter tkinter

How to change the plot color after a button press in matplotlib/tkinter?


You need to change the color of the Line2D object created by ax.plot.Store it in self, then you will be able to access it in your action handler.

def __init__(self, master):    ...    # ax.plot returns a list of lines, but here there's only one, so take the first    self.line = self.ax.plot(x, y, color='red')[0]

Then, you can change said line's color in your handler. You need to call canvas.draw to force the line to be re-rendered.

def change_to_blue(self):    self.line.set_color('blue')    self.canvas.draw()