簡體   English   中英

Matplotlib:在3D散點圖onpick中更改點的顏色

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

我正在繪制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()

散點圖的所有點都只有一種顏色。 因此,您無法更改僅包含一行的數組的第五行。 因此,首先您需要確保通過facecolors參數分別設置了facecolors。 然后,您可以以陣列格式獲得它們,一旦單擊發生,請提供原始陣列的副本,並將相應元素更改為散點圖。

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

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