简体   繁体   English

Matplotlib:在3D散点图onpick中更改点的颜色

[英]Matplotlib: Change color of point in 3D scatter plot onpick

I am rendering a 3D scatter plot and want to change the color of the points to red when they are clicked. 我正在绘制3D散点图,并希望在单击点时将其颜色更改为红色。 When a new point is clicked, I want the color of the previously-clicked points to go back to the default. 单击新点时,我希望先前单击的点的颜色恢复为默认值。 So far, I cannot even get matplotlib to change the color of the point when clicked. 到目前为止,我什至无法获得matplotlib来更改单击时该点的颜色。 Does anyone know what is wrong, or if this is not supported for 3D scatter plots? 有谁知道这是什么问题,或者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()

The scatter only has a single color for all points. 散点图的所有点都只有一种颜色。 So you cannot change the 5th row of an array which only has a single row. 因此,您无法更改仅包含一行的数组的第五行。 So first you need to make sure the facecolors are all set individually via the facecolors argument. 因此,首先您需要确保通过facecolors参数分别设置了facecolors。 Then you can obtain them in an array format and once a click happens, supply a copy of the original array with the respective element changed to the scatter. 然后,您可以以阵列格式获得它们,一旦单击发生,请提供原始阵列的副本,并将相应元素更改为散点图。

2D 2D

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()

3D 3D

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM