如何在Matlab图形中添加工具提示或覆盖文本

use*_*193 2 matlab overlay mouseover tooltip matlab-figure

我有两行或多行的图形。这些行具有与之关联的其他重要信息,例如平均要创建该行的数据点数等等。我想在图中访问这些信息。

我认为对此的一个好的解决方案是,如果您可以将鼠标悬停在一行上并获取扩展的信息。

然而,搜索工具提示/叠加/将鼠标悬停在数字上似乎没有收获。

例:

figure; hold on;
plot(1:10,rand(10,1))
plot(1:10,rand(10,1))
% additional info
plot_1_info.name = 'Alice';
plot_2_info.name = 'Bob';
plot_1_info.age = 24;
plot_2_info.age = 12;
Run Code Online (Sandbox Code Playgroud)

有什么好的解决方案或更好的方法吗?

Wol*_*fie 5

您可以更改数据游标的行为,此选项具有良好的向后兼容性(我已经在R2017b中测试了以下内容,之前在15b中使用过类似功能)。

数据游标

查看我的评论以获取详细信息:

% Create some data
x = (1:2:20).';
y = rand(10,1);
name = { 'Alice'; 'Alice'; 'Alice'; 'Alice'; 'Bob'; 'Bob'; 'Bob'; 'Chris'; 'Chris'; 'Chris' };
age = [ 24; 24; 24; 24; 12; 12; 12; 17; 17; 17 ];
% Put it in a table, so we have it all together for indexing as plot data
tbl = table( x, y, name, age );

% Create the plot, assign the UserData property to the plot object
f = figure; 
plt = plot( x, y );
plt.UserData = tbl;

% Hijack the Data Cursor update callback so we can inject our own info
dcm = datacursormode( f );
set( dcm, 'UpdateFcn', @onDataCursor );

% Function which returns the text to be displayed on the data cursor
function txt = onDataCursor( ~, evt )
    % Get containing figure
    f = ancestor( evt.Target, 'figure' );
    % Get the index within the original data
    idx = getfield( getCursorInfo( datacursormode( f ) ), 'DataIndex' );
    % The original data is stored in the UserData property
    data = evt.Target.UserData;
    % Each element of the cell array is a new line on the cursor
    txt = { sprintf( 'X: %g', data.x(idx) ), ...
            sprintf( 'Y: %g', data.y(idx) ), ...
            sprintf( 'Name: %s', data.name{idx} ), ...
            sprintf( 'Age: %g', data.age(idx) ) };          
end
Run Code Online (Sandbox Code Playgroud)

输出:

用数据提示绘图

注意:在任何情况下,如果有多个数据光标提示,我都不会处理。您可以idx在回调中轻松实现循环以处理此问题,我将其保留为练习。


这种方法非常灵活。例如,如果我们有3行(每个“人”一行),那么他们可以各自拥有自己的UserData结构,并且我们不需要在表行中重复所有信息。

A = struct( 'X', 1:4, 'Y', rand(1,4), 'Name', 'Alice', 'Age', 24 );
B = struct( 'X', 1:3, 'Y', rand(1,3), 'Name', 'Bob', 'Age', 12 );
C = struct( 'X', 1:3, 'Y', rand(1,3), 'Name', 'Chris', 'Age', 17 );

f = figure; hold on;
plt = plot( A.X, A.Y ); plt.UserData = A;
plt = plot( B.X, B.Y ); plt.UserData = B;
plt = plot( C.X, C.Y ); plt.UserData = C;

% ... Now the struct fields can be accessed from the callback
Run Code Online (Sandbox Code Playgroud)


Dev*_*-iL 5

使用R2019a中引入的新数据提示定制系统,我们可以执行以下操作:

figure(); hP = plot(1:10,rand(10,1),1:10,rand(10,1));
nPts = cellfun(@numel, {hP.XData});
hP(1).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Name', repmat("Alice",nPts(1),1) );
hP(1).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Age',  repmat(12,     nPts(1),1) );
hP(2).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Name', repmat("Bob",  nPts(2),1) );
hP(2).DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Age',  repmat(24,     nPts(2),1) );
% (Of course the above can be organized in a nicer way using a function)
Run Code Online (Sandbox Code Playgroud)

其产量:

在此输入图像描述

请注意,“悬停数据提示”具有黑色文本,而“点击数据提示”具有蓝色文本 - 这是默认行为。