Adding extra information to datapoints on a plot
Asked Answered
D

2

2

THERE IS THE SOLUTION BELOW!

enter image description here

This (x,y) value actually corresponds to a t value, which I want to see on the plot. What can I do?

Clearly matlab has the ability to display multiple information in the datapoint box:

enter image description here just call it as "TimePlot(x,y,t)" and it will work. This code, I believe, also illustrates a few key points in modifying datatips.

function  TimePlot( varargin )
x=varargin{1};
y=varargin{2};
t=varargin{nargin};
fh=figure;
plot(varargin{1:nargin-1})

function output_txt = myfunction(obj,event_obj)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% output_txt   Data cursor text string (string or cell array of strings).




pos = get(event_obj,'Position');

ind=intersect(Find(x,pos(1),1e-10),Find(y,pos(2),1e-10));
if(length(ind)~=1)
    text='err';
else
    text=num2str(t(ind),4);
end

output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)],['T: ',text]};

% If there is a Z-coordinate in the position, display it as well
if (length(pos) > 2)
    output_txt{end+1} = ['Z: ',num2str(pos(3),4)];
end

end

dcm=datacursormode(fh);
datacursormode on
set(dcm,'updatefcn',@myfunction)



end

function [ out ] = Find( vector, value ,precision)

if nargin < 3
    precision =   0.0001;
end

out=[];

for i=1:length(vector)
    
    if(abs(vector(i)-value)<precision)
        out=[out i];
    end

end

end
Duckweed answered 26/5, 2015 at 20:14 Comment(3)
Please be more specific on how you want to accomplish this. If you have a more specific coding question, edit your question. Otherwise it is off topic for StackOverflow (you might instead be able to get help on SuperUser)Stoush
Here is a tutorial on how to do this.Soulier
or cunsult this duplicate question Data tip customization in matlab figureSoulier
S
2

On MATLAB Central you can find an extensive video tutorial on how to create custom data tips: Tutorial: How to make a custom data tip in MATLAB.

If you use the standard data tip in MATLAB, it will annotate the X and Y value of a data point. This video will show how to customize the information that is shown in that data tip.

In the documentation about the datacursormode you find some more examples (the following all copied from doc):

This example enables data cursor mode on the current figure and sets data cursor mode options. The following statements

  • Create a graph
  • Toggle data cursor mode to on
  • Obtain the data cursor mode object, specify data tip options, and get the handle of the line the data tip occupies:
fig = figure;
z = peaks;
plot(z(:,30:35))
dcm_obj = datacursormode(fig);
set(dcm_obj,'DisplayStyle','datatip',...
    'SnapToDataVertex','off','Enable','on')

disp('Click line to display a data tip, then press Return.')
% Wait while the user does this.
pause 

c_info = getCursorInfo(dcm_obj);
% Make selected line wider
set(c_info.Target,'LineWidth',2)

enter image description here

This example shows you how to customize the text that the data cursor displays. For example, you can replace the text displayed in the data tip and data window (x: and y:) with Time: and Amplitude: by creating a simple update function.

Save the following functions in your current directory or any writable directory on the MATLAB path before running them. As they are functions, you cannot highlight them and then evaluate the selection to make them work.

Save this code as doc_datacursormode.m:

function doc_datacursormode % Plots graph and sets up a custom data tip update function 
fig = figure; 
a = -16; 
t = 0:60; 
plot(t,sin(a*t)) 
dcm_obj = datacursormode(fig); 
set(dcm_obj,'UpdateFcn',@myupdatefcn)

Save the following code as myupdatefcn.m on the MATLAB path:

function txt = myupdatefcn(empt,event_obj) % Customizes text of data tips

pos = get(event_obj,'Position'); txt = {['Time: ',num2str(pos(1))],...
          ['Amplitude: ',num2str(pos(2))]};

To set up and use the update function, type:

doc_datacursormode

When you place a data tip using this update function, it looks like the one in the following figure.

enter image description here

Soulier answered 26/5, 2015 at 20:59 Comment(0)
R
0

I do not think you can see the value of third dimension in a two dimensional plot. Can you try doing surf(x,y,t) or plot3(x,y,t) so that you get a three dimensional plot and with proper orientation you can get required plot and all the required x, y and t values.

Rightness answered 26/5, 2015 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.