Data tip when using two y-Axes
5 views (last 30 days)
Show older comments
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')
0 Comments
Accepted Answer
Benjamin Kraus
on 31 Mar 2022
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
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) = [];
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!