Matlab plot enhancements for value showing

Is there a way we can change what we see currently when clicking on a particular point in the plot?
When I click on a point in the graph I see X 1 and Y 0.1335 but I am interested in showing only A = 0.1335 instead and likewise if it is a B point then the same

답변 (1개)

Walter Roberson
Walter Roberson 2023년 3월 5일

0 개 추천

If you are using datacursormode then supply a custom update function that returns a cell array of character vectors, one cell for each output line.
Otherwise: particular kinds of plots called "charts" support a newer mechanism that allows a different method of customization; see https://www.mathworks.com/help/matlab/creating_plots/create-custom-data-tips.html

댓글 수: 6

MattC
MattC 2023년 3월 5일
Thanks for getting back to me on this @Walter Roberson. I checked both the options you shared and there isn't a way to customize the datatip legends. I do not want to hard code that. For my case I need the labels to show the value for whatever point on the plot is selected. Is it possible to do that?
Supppose that you used
h = plot(x, a);
then you would do
h.DataTipTemplate.DataTipRows(2:end) = [];
h.DataTipTemplate.DataTipRows(1).Label = "A = ";
h.DataTipTemplate.DataTipRows(1).Value = 'YData';
So, I am using the subplot option as I need multiple plots and this would not work for me because my plot has 3 values A,B,C which I then want to use as legends itself and then the corresponding value to it. So, it's not always A=, it can also be B= and C=
h.DataTipTemplate.DataTipRows(1).Label = "A = ";
h.DataTipTemplate.DataTipRows(1).Value = 'YData';
My code:
axeshandle(1)=subplot('position',[0.13 .808 .70 .09]);
grid on
box on
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize',4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
xlim([0 3])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'LineWidth',1, 'DisplayName','Threshold');
legend([hp0{1,:} yl], 'Location','eastoutside');
axeshandle(1)=subplot('position',[0.13 .808 .70 .09]);
grid on
box on
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
row = dataTipTextRow(xtl{k2} + " = ", 'YData');
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize',4, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
hp1{k1,k2}.DataTipRows = row;
hp2{k1,k2}.DataTipRows = row;
end
end
xlim([0 3])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'LineWidth',1, 'DisplayName','Threshold');
legend([hp0{1,:} yl], 'Location','eastoutside');
You might possibly need to create a datatip() object.
MattC
MattC 2023년 3월 6일
I tried doing that and looks like it messes up with my data point and the chart itself. I cannot see any of my datapoints, legends, and it did not show the value upon clicking the point as well
I tested with
h(1) = plot(rand(1,20), 'DisplayName', 'A');
hold on
h(2) = plot(rand(1,20), 'DisplayName', 'B');
hold off
row1 = dataTipTextRow('A' + " = ", 'YData');
row2 = dataTipTextRow('B' + " = ", 'YData');
h(1).DataTipTemplate.DataTipRows = row1;
h(2).DataTipTemplate.DataTipRows = row2;
legend(h);
and then I used the axis toolbare to turn on datatips for the axes. Everything looked fine, with the datatip moving to the closest point to where I clicked, and using the proper "A = " or "B = " before the value.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2023년 3월 5일

댓글:

2023년 3월 6일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by