matlab - 如何使用切换按钮打开/关闭散点图中的标签
问题描述
我试图为每个点创建一个带有标签的散点图:
到目前为止,我的代码如下所示:
x = rand(1,100); y = rand(1,100); pointsize = 30;
idx = repmat([1 : 10], 1, 10) % I used class memberships here
figure(1)
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
Button = uicontrol('Parent',figure(2),'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',{@pb_call, MDSnorm, ???});
在我的脚本结束时,我尝试定义pb_call
函数。我尝试了几个不同的版本,都失败了。
我有一个粗略的想法,我需要做什么。就像是:
function [] = pb_call( ??? )
if get(Button, 'Value')
T --> invisible % ???
else
T --> visible % ???
end
end
如何修改上述内容以根据需要打开或关闭标签?
解决方案
这是一个工作示例:
x = rand(1,9); y = rand(1,9); pointsize = 30;
idx = repmat(1 : 3, 1, 3); % I used class memberships here
labels = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j'};
h_fig = figure(1); %Create a figure, an keep the handle to the figure.
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
%Add a button to the same figure (the figure with the labels).
Button = uicontrol('Parent',h_fig,'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',@pb_call);
function pb_call(src, event)
%Callback function (executed when button is pressed).
h_fig = src.Parent; %Get handle to the figure (the figure is the parent of the button).
h_axes = findobj(h_fig, 'Type', 'Axes'); %Handle to the axes (the axes is a children of the figure).
h_text = findobj(h_axes, 'Type', 'Text'); %Handle to all Text labels (the axes is the parent of the text labels).
%Decide to turn on or off, according to the visibility of the first text label.
if isequal(h_text(1).Visible, 'on')
set(h_text, 'Visible', 'off'); %Set all labels visibility to off
else
set(h_text, 'Visible', 'on'); %Set all labels visibility to on
end
end
解释在评论中。
推荐阅读
- .net-core - .netcore 3.1 为一项功能加载多个 SO 文件
- python - 如何阻止 pycharm 使 RAM 过载?
- python-2.7 - 在 Python 2.7 环境中安装 dynd-python 时出现分段错误错误
- marklogic - 使用 MarkLogic REST API 将文本文件加载为二进制文件
- c - 在 C 编程和使用 malloc 中,返回指针的指针函数中的 free() 在哪里?
- node.js - 在受保护的 Google Cloud 功能上无法通过 CORS 错误
- c# - Onclick 动作不去方法
- odbc - 在 Netezza 中加载 UCS-2LE 文件
- matlab - 功能分化
- javascript - React JSX 表达式在添加第二个时必须有父元素错误