Data tip when using two y-Axes

7 views (last 30 days)
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')

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 31 Mar 2022
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 Comments
Benjamin Kraus
Benjamin Kraus on 1 Apr 2022
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 on 1 Apr 2022
Edited: Konvictus177 on 1 Apr 2022
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!