Matplotlib – click and change color randomly

# click and change color randomly

import matplotlib.pyplot as plt
from matplotlib.backend_bases import MouseButton
import random


x = [1, 2, 3, 4]
y = [5, 6, 7, 8]

def on_click(event):
    if event.button is MouseButton.LEFT:
        print(event.xdata, event.ydata)
        r = random.random()
        g = random.random()
        b = random.random()
        plt.plot(x, y, 'o', color=(r, g, b))

plt.connect('button_press_event', on_click)
plt.plot(x,y, 'ro')

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s