如何在Matlab图形中添加工具提示或覆盖文本 [英] How to add tooltips or overlay text in a Matlab figure

查看:257
本文介绍了如何在Matlab图形中添加工具提示或覆盖文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两行或更多行的图形.这些行具有与之相关的其他重要信息,例如平均要创建该行的数据点数等等.我想在图中访问此信息.

I have a figure with two or more lines. These lines have additional, important information associated with them, like how many data points were averaged to create the line etc. I would like to access this information in my figure.

我认为,如果您可以用鼠标悬停在一行上并获取扩展的信息,那么这是一个很好的解决方案.

I thought a good solution for this would be to if you could hover over a line with your mouse and get that extended information.

但是,在图形上搜索工具提示/叠加层/鼠标悬停似乎并没有取得成功.

However searching for tooltips/overlays/hover-over on figures seemed to not be fruitful.

示例:

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;

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

Any good solutions or better approaches for this?

推荐答案

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

You can change the data cursor behaviour, this option has good backwards compatability (I've tested the below in R2017b, used similar before in 15b).

有关详细信息,请参阅我的评论:

See my comments for details:

% 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

输出:

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

Note: I've not handled anywhere the case that there is more than one data cursor tip. You can easily implement a loop over idx within the callback to handle this, I leave it as an exercise.

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

This approach is really flexible. For instance if we had 3 lines (one per 'person') then they can each have their own UserData struct, and we don't need to repeat all the info in table rows.

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

这篇关于如何在Matlab图形中添加工具提示或覆盖文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆