# Data tip when using two y-Axes

8 views (last 30 days)
Konvictus177 on 31 Mar 2022
Edited: Konvictus177 on 1 Apr 2022
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 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);
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.

### Community Treasure Hunt

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

Start Hunting!

Translated by