Python、Matplotlib、散点图、更改单击点的颜色 [英] Python, Matplotlib, Scatter plot, Change color on the clicked point
本文介绍了Python、Matplotlib、散点图、更改单击点的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有选择器事件的简单散点图.
我想更改我用鼠标单击的数据点的颜色.
我拥有的代码将更改整个数组的颜色.
我该如何更改一个特定点?
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 array.
How can I just change one particular point?
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()
sctPlot, = ax.plot(testData[:,0], testData[:,1], "o", picker = 5)
plt.grid(True)
plt.axis([-0.5, 0.5, -0.5, 0.5])
def on_pick(event):
artist = event.artist
artist.set_color(np.random.random(3))
print "click!"
fig.canvas.draw()
fig.canvas.mpl_connect('pick_event', on_pick)
推荐答案
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()
这篇关于Python、Matplotlib、散点图、更改单击点的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文