我正在渲染3D散点图,并希望在单击点时将其颜色更改为红色。单击新点时,我希望先前单击的点的颜色恢复为默认值。到目前为止,我什至无法获得matplotlib来更改单击时该点的颜色。有谁知道哪里出了问题,或者3D散点图不支持此操作?
points = ax1.scatter(X_t[:,0], X_t[:,1], X_t[:,2], picker=True)
def plot_curves(indexes):
for i in indexes: # might be more than one point if ambiguous click
points._facecolors[i,:] = (1, 0, 0, 1)
points._edgecolors[i,:] = (1, 0, 0, 1)
plt.draw()
def onpick(event):
ind = event.ind
plot_curves(list(ind))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
答案 0 :(得分:0)
散点图的所有点只有一种颜色。因此,您无法更改仅包含一行的数组的第五行。因此,首先您需要确保通过facecolors参数分别设置了facecolors。 然后,您可以以数组格式获取它们,一旦单击,请提供原始数组的副本,并将相应元素更改为散点图。
import numpy as np
import matplotlib.pyplot as plt
X_t = np.random.rand(10,4)*20
fig, ax1 = plt.subplots()
points = ax1.scatter(X_t[:,0], X_t[:,1],
facecolors=["C0"]*len(X_t), edgecolors=["C0"]*len(X_t), picker=True)
fc = points.get_facecolors()
def plot_curves(indexes):
for i in indexes: # might be more than one point if ambiguous click
new_fc = fc.copy()
new_fc[i,:] = (1, 0, 0, 1)
points.set_facecolors(new_fc)
points.set_edgecolors(new_fc)
fig.canvas.draw_idle()
def onpick(event):
ind = event.ind
plot_curves(list(ind))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X_t = np.random.rand(10,4)*20
fig, ax1 = plt.subplots(subplot_kw=dict(projection="3d"))
points = ax1.scatter(X_t[:,0], X_t[:,1], X_t[:,2],
facecolors=["C5"]*len(X_t), edgecolors=["C5"]*len(X_t), picker=True)
fc = points.get_facecolors()
def plot_curves(indexes):
for i in indexes: # might be more than one point if ambiguous click
new_fc = fc.copy()
new_fc[i,:] = (1, 0, 0, 1)
points._facecolor3d = new_fc
points._edgecolor3d = new_fc
fig.canvas.draw_idle()
def onpick(event):
ind = event.ind
plot_curves(list(ind))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()