[英]How to toggle points on and off on matplotlib 3D scatter plot?
SOLVED (see below)已解决(见下文)
On 2D matpotlib scatter plot I can turn on and off points by accessing _offsets
property of scatter plot object and setting it's .mask
attribute True/False for indexes of those points we want to show/hide like this:在2D matpotlib scatter plot 上,我可以通过访问 scatter plot
_offsets
的属性的.mask
属性来打开和关闭点,并将其设置为我们想要显示/隐藏索引的 True/False 点。
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import random
def TogglePoints(event, plot):
plot._offsets.mask = [ [1, 1], [1, 1], [1, 1], [0, 0], [0, 0], [0, 0] ]
plot.figure.canvas.draw()
x = [random.randint(-10, 10) for n in range(6)]
y = [random.randint(-10, 10) for n in range(6)]
ax = plt.axes()
sc = ax.scatter(x, y, marker='o', s=20, alpha=1)
ax_button = plt.axes([0.81, 0.01, 0.1, 0.05])
button= Button(ax_button, "Toggle")
button.on_clicked(lambda event: TogglePoints(event, sc))
plt.show()
When you click the "Toggle" button on the figure, points with indexes 0, 1, 2 will disappear.当您单击图中的“切换”按钮时,索引为 0、1、2 的点将消失。 You can make them re-appear by setting
_offsets.mask
back to False and re-drawing plot.您可以通过将
_offsets.mask
设置回 False 并重新绘制 plot 来使它们重新出现。 This is what I want to achieve with matplotlib 3D scatter plot.这就是我想用 matplotlib 3D scatter plot 来实现的。
Using _offsets.mask = [ [1, 1], [1, 1], [1, 1], [0, 0], [0, 0], [0, 0] ]
on 3D scatter plot doesn't seem to work.在 3D 上使用
_offsets.mask = [ [1, 1], [1, 1], [1, 1], [0, 0], [0, 0], [0, 0] ]
散点图 plot 似乎没有去工作。 Actually it alters type of underlying array from MaskedArray to numpy.ndarray for some reason (see: Numpy MaskedArray in matplotlib 3D scatter plot, turns into ndarray when called by PyQt5 button click ). Actually it alters type of underlying array from MaskedArray to numpy.ndarray for some reason (see: Numpy MaskedArray in matplotlib 3D scatter plot, turns into ndarray when called by PyQt5 button click ). I know that 3D scatter plots have
_offsets3d
property.我知道 3D 散点图具有
_offsets3d
属性。 However I don't know how I can use it to show/hide points on the plot.但是我不知道如何使用它来显示/隐藏 plot 上的点。 Or maybe there's some other way?
或者也许还有其他方法?
Does anyone know how I can do that?有谁知道我该怎么做?
Thanks to this post:感谢这篇文章:
Get working alpha value of scatter points in mpl_toolkits.basemap.Basemap 获取 mpl_toolkits.basemap.Basemap 中散点的工作 alpha 值
I've found a workaround that serves my purpose.我找到了适合我的目的的解决方法。 It concerns setting alpha values of points with
set_facecolors()
.它涉及使用
set_facecolors()
设置点的 alpha 值。 So the working code now looks like this:所以工作代码现在看起来像这样:
...
import pandas as pd #added
def TogglePointsOFF(event, plot):
for n in range(3): # n = index of point
fc_colors[n, 3] = 0 # 4th value is alpha
plot.set_facecolors(fc_colors)
plot.figure.canvas.draw()
def TogglePointsON(event, plot):
for n in range(3): # n = index of point
fc_colors[n, 3] = 1 # 4th value is alpha
plot.set_facecolors(fc_colors)
plot.figure.canvas.draw()
#I've put it into DataFrame() so you can better see
df = pd.DataFrame()
df['label'] = ["data_"+str(n) for n in range(6)]
df['id'] = [1, 1, 1, 2, 2, 2]
['x'] = [random.randint(-10, 10) for n in range(6)]
['y'] = [random.randint(-10, 10) for n in range(6)]
['z'] = [random.randint(-10, 10) for n in range(6)]
colors = {1:'red', 2:'blue'} # to map colors with df 'id'
#plot points colored according to value of df['id']
ax = plt.axes()
sc = ax.scatter(df['x'], df['y'], df['z'], c=df['id'].map(colors), marker='o', s=20, depthshade=False)
global fc_colors #yeah yeah globals...
face_colors = sc._face_colors
ax_button = plt.axes([0.81, 0.01, 0.1, 0.05])
ax_button_1 = plt.axes([0.68, 0.01, 0.12, 0.05])
button= Button(ax_button, "OFF")
button_1= Button(ax_button_1, "ON")
button.on_clicked(lambda event: TogglePointsOFF(event, sc))
button_1.on_clicked(lambda event: TogglePointsON(event, sc))
plt.show()
Clicking buttons "ON" and "OFF" will hide/show group of points based on index.单击“ON”和“OFF”按钮将隐藏/显示基于索引的点组。
I've tried using set_alpha()
and passing iterable of alpha values like: [0, 0, 0, 1, 1, 1]
however it seemed to work on random points and set alpha of incorrect points.我尝试使用
set_alpha()
并传递可迭代的 alpha 值,例如: [0, 0, 0, 1, 1, 1]
但它似乎适用于随机点并设置不正确点的 alpha。
Also getting face_colors from get_facecolors()
seemed to get colors with random index alignment.从
get_facecolors()
获取 face_colors 似乎也得到了 colors 和随机索引 alignment。 This may be connected why passing iterable with alpha values to set_alpha()
didn't work.这可能与为什么将带有 alpha 值的可迭代对象传递给
set_alpha()
不起作用有关。 That's why I take colors of points from: sc._face_colors
.这就是为什么我从以下位置获取 colors 的点:
sc._face_colors
。
Thank you for your time.感谢您的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.