Python, Matplotlib, Scatter plot, Change color on the clicked point

前端 未结 1 1830
庸人自扰
庸人自扰 2021-01-18 09:00

I have a simple scatter plot with a picker event.
I want to change the color of the data point I click with mouse.
The code I have will change the color of the whole

相关标签:
1条回答
  • 2021-01-18 09:26
    import sys
    import numpy as np
    import matplotlib.pyplot as plt
    
    testData = np.array([[0,0], [0.1, 0], [0, 0.3], [-0.4, 0], [0, -0.5]])
    fig, ax = plt.subplots()
    coll = ax.scatter(testData[:,0], testData[:,1], color=["blue"]*len(testData), picker = 5, s=[50]*len(testData))
    plt.grid(True)
    plt.axis([-0.5, 0.5, -0.5, 0.5])
    
    def on_pick(event):
        print testData[event.ind], "clicked"
        coll._facecolors[event.ind,:] = (1, 0, 0, 1)
        coll._edgecolors[event.ind,:] = (1, 0, 0, 1)
        fig.canvas.draw()
    
    fig.canvas.mpl_connect('pick_event', on_pick)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题