Data tip when using two y-Axes

조회 수: 4 (최근 30일)
Konvictus177
Konvictus177 2022년 3월 31일
편집: Konvictus177 2022년 4월 1일
Hi,
I have two y-axis. How can I display both y-values when I create a data tip?
Thanks.
aMatrix = rand(20,30);
yAxis1 = 32.*(1:size(aMatrix,1));
yAxis2 = 165.*(1:size(aMatrix,1));
xAxis = 1:size(aMatrix,2);
yyaxis left
imagesc(xAxis, yAxis1, aMatrix);
ax = gca;
ax.YColor = ax.XColor;
ylabel('Left Side')
yyaxis right
imagesc(xAxis, yAxis2,aMatrix);
ax.YColor = ax.XColor;
ylabel('Right Side')

채택된 답변

Benjamin Kraus
Benjamin Kraus 2022년 3월 31일
You can add anything you want to the data tips by modifying the DataTipTemplate.
It is a bit tricky to do with images, because you need to specify one value for every pixel of the image, but I think this will do basically what you were asking:
aMatrix = rand(20,30);
yAxis1 = 32.*(1:size(aMatrix,1));
yAxis2 = 165.*(1:size(aMatrix,1));
xAxis = 1:size(aMatrix,2);
yyaxis left
imagesc(xAxis, yAxis1, aMatrix);
ax = gca;
ax.YColor = ax.XColor;
ylabel('Left Side')
yyaxis right
im = imagesc(xAxis, yAxis2,aMatrix);
ax.YColor = ax.XColor;
ylabel('Right Side')
% You need to create a datatip first before you can set the DataTipTemplate
% on an image. You can delete it immediately after it is created.
dt = datatip(im);
delete(dt);
% Create a matrix the same size as the image data to use in the data tip.
yAxis1DataTip = yAxis1'+zeros(size(aMatrix));
yAxis2DataTip = yAxis2'+zeros(size(aMatrix));
% Add two rows to the data tip, one that shows Y1, and the other that shows
% Y2.
im.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("Y1",yAxis1DataTip);
im.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("Y2",yAxis2DataTip);
  댓글 수: 3
Benjamin Kraus
Benjamin Kraus 2022년 4월 1일
You need one value per pixel in the image.
Your code is creating two values per pixel in the image:
[xAxisDataTip, yAxis1DataTip]
What you can do is create your own strings, then have one string per pixel:
string(xAxisDataTip) + ", " + string(yAxis1DataTip);
The full code would look like this:
aMatrix = rand(20,30);
yAxis1 = 32.*(1:size(aMatrix,1));
yAxis2 = 165.*(1:size(aMatrix,1));
xAxis = 1:size(aMatrix,2);
yyaxis left
im1 = imagesc(xAxis, yAxis1, aMatrix);
ax = gca;
ax.YColor = ax.XColor;
ylabel('Left Side')
yyaxis right
im2 = imagesc(xAxis, yAxis2, aMatrix);
ax.YColor = ax.XColor;
ylabel('Right Side')
% You need to create a datatip first before you can set the DataTipTemplate
% on an image. You can delete it immediately after it is created.
dt = datatip(im2);
delete(dt);
% Now customize the data tip.
xAxisDataTip = xAxis+zeros(size(aMatrix));
yAxis1DataTip = yAxis1'+zeros(size(aMatrix));
yAxis2DataTip = yAxis2'+zeros(size(aMatrix));
im2.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("X1 Y1",string(xAxisDataTip) + ", " + string(yAxis1DataTip));
im2.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("X2 Y2",string(xAxisDataTip) + ", " + string(yAxis2DataTip));
im2.DataTipTemplate.DataTipRows(1) = [];
Konvictus177
Konvictus177 2022년 4월 1일
편집: Konvictus177 2022년 4월 1일
Thank you. Exactly what I was looking for!
I still have an issue with only one y axis being zoomed when I use zoom but I think this is slightly going off topic here.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by