How can we see yticks below zero as positive and keep labels updated when zooming in?
2 views (last 30 days)
Show older comments
I'm trying to plot head-to-tail mass spectra of two variables mstable1 and mstable2. Both variables contain m/z(mass channel) in the first column and intensity in the second column. When I use the code below, it's showing negative values on the yaxis below zero. But, I want it to be positive.
figure;
stem(mstable1{1,5}(:,1), (-1)*mstable1{1,5}(:,2),'marker','none','LineWidth',1.5);
hold on
stem(mstable2{1,5}(:,1), mstable2{1,5}(:,2),'marker','none','LineWidth',1.5);
hold off
xlabel('m/z')
ylabel('Intensity')
set(gca,'FontSize',18,'layer','top','LineWidth',2,'box','on','YMinorTick','on');

When I add the code below to my plot, it changes the negative y values to positive but when I zoom in the labels are not updated.
% Try 1
yticks_val = yticks;
yticklabels(compose('%.e', abs(yticks_val)));
% Try 2
ax = gca;
oldYLabels = str2double(ax.YTickLabel);
yticklabels(abs(oldYLabels));
% I tried two different ways


How can I change y values to positive and update the labels when I zoom in?
0 Comments
Answers (2)
Steven Lord
on 1 May 2025
You could use the zoom mode object returned from the zoom function to specify an ActionPostCallback that will be called after MATLAB zooms in or out. For example, this one sets the Z tick labels to be the square of the Z tick locations.
surf(peaks);
Z = zoom(gcf)
Z.ActionPostCallback = @(varargin) zticklabels(string(zticks.^2));
Now zoom in and out and notice that the ticks along the Z axis are all the squares of the locations at which they're located.
Alternately you might want to check the LimitsChangedFcn property of the axes ruler objects (which you can retrieve from the axes using the XAxis, YAxis, ZAxis, etc. properties) and use that to recalculate and/or reformat the axes labels. See the Callbacks section on this documentation page for more information. This could help if you're planning not only to zoom into the axes but also to pan.
0 Comments
See Also
Categories
Find more on Data Exploration 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!