Adding extra information to datapoints on a plot

主宰稳场 提交于 2019-12-24 13:43:26

问题


THERE IS THE SOLUTION BELOW!

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:

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

回答1:


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)

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.




回答2:


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.



来源:https://stackoverflow.com/questions/30468035/adding-extra-information-to-datapoints-on-a-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!