How can I have unique datacursor updatefcn for different subplots in a single figure
2 次查看(过去 30 天)
显示 更早的评论
I am trying to implement a unique update function call for two subplots I have in a single figure. I want the datatip to show information that is unique to the dataset shown in the different axes. The update function is the same for the two subplots, but requires different values for the function inputs. I am not able to get this to work. Please advise. I am using Matlab 8.1.0.604 on a Mac.
A description of the code I am using and simplified code are shown below
For example, I have two bar plots that show y1 (subplot(2,1,1)) and y2 (subplot(2,1,2)). The x values for both plots are integers 1:n. I want to display yi and a label in the datatip. I make the bar plot, call datacursormode, and set the 'updatefcn' for both plots as I create them. However, when I go to the figure, the datatip only works for the second axes. I can make it work on the first axes if I don't call the datacursor mode for the next axis.
Here is a simplified version of the code I am using where x is a double array and y and labels are each cells of double arrays. 'labeldtips' is the update function.
function plot_example(x,y1,y2,labels1,labels2) h=figure(1); h1=subplot(1,2,1); h2=subplot(1,2,2);
bar(h1,1:length(x),cell2mat(y1)','stacked') hdt1 = datacursormode; set(hdt1,'UpdateFcn',{@labeldtips,cell2mat(y1),cell2mat(wt1),labels1},'DisplayStyle','window')
bar(h2,1:length(x),cell2mat(y2)'*2,'stacked') hdt2 = datacursormode; set(hdt2,'UpdateFcn',{@labeldtips,cell2mat(y2)*2,cell2mat(wt2),labels2},'DisplayStyle','window')
function output_txt = labeldtips(obj,event_obj,ydata,labels) pos = get(event_obj,'Position'); x = pos(1); y = pos(2);
%Create growing sum, due to the 'stacked' nature of the bar plot ydata_sum = cumsum(ydata,1);
% Identify index idx = find(ydata_sum(:,x) == y,1); % Find index to retrieve obs. name output_txt{1} = labels{x}(idx); output_txt{end+1} = ['Percent of Type: ',num2str(ydata(idx,x))]; So that you can reproduce the problem, here are possible x,y,wt and label arrays:
x=1:3;
y1={rand(3,1) rand(3,1) rand(3,1)};
y2={rand(3,1) rand(3,1) rand(3,1)};
labels1={['a' 'b' 'c']; ['d' 'e' 'f']; ['h' 'i' 'j'];}
labels2=labels1;
0 个评论
回答(1 个)
Jasper van Casteren
2018-11-22
I use
fig = figure;
ax1 = subplot(whatever);
ax2 = subplot(whatever);
datacursor = datacursormode(fig);
datacursor.set('UpdateFcn',@MyFunc);
% MyFunc must be an embedded function to know ax1 and ax2
function MyFunc(Target, evtObj)
if Target.Parent==ax1
whatever;
else
whatever;
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Visual Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!