I am trying to add a secondary y-axis with different units to a 3D plot.
[m2_array, ~ , ~] = F_readBin('amb.bin');
amb = m2_array(:,:,lat);
surfc(light,'LineWidth',0.001);
ylim([1 24]); xlim([1 size(light,2)]); title(['@ ',num2str(lat),'°N']);
xticks([0:50:size(m2_array,2)]);
labels=cellstr(num2str((day_start:50:day_end)')); xticklabels(labels);
xlabel('Season days'); ylabel('Daytime{[hours]}');zlabel('surface light
[\mumol m^2 s^-^1]')
colormap winter;
However,all solutions I can find e.g. yyaxis seem to work only for 2D plots. Is there a work around for surf, mesh, surfc plots?
Not sure if this is what you are looking for, but I guess a basic approach to adding secondary axes to a 3D plot would be the same as for 2D (as far as I know a 2D plot in matlab is just a 3D-plot viewed from above).
The idea is that you place a second set of axes on top of the first, then tweak it to fit your requirements, e.g. by hiding unused axes and making the secondary background transparent. This is explained in the Matlab documentation here.
For 3D this is a bit tricky because of the default axis and label locations, but that's where undocumentedmatlab comes to the rescue. Using the FirstCrossOverValue
and SecondCrossoverValue
properties of the Axes
' NumericRuler
objects (XAxis
, YAxis
, ZAxis
), we can position the secondary axis in the desired location.
The basic idea is illustrated in the example below. This is for the z-axis, but the same approach can be used for y or x.
clear; close all; clc
% Dummy data from matlab example
[X,Y,Z] = peaks(25);
% Primary axes with some arbitrary viewpoint and labels
hs = surf(X,Y,Z); % Get surface object
ha = hs.Parent; % Get parent Axes
ha.View = [25, 40]; % Set arbitrary view point
xlabel 'xa';
ylabel 'ya';
zlabel 'za';
grid on
% Secondary axes on top of primary, using same view point
hb = axes('view',ha.View);
hb.ZLim = [0 7]; % Arbitrary axis limits
zlabel 'zb';
% Hide secondary background and x and y rulers
hb.Color = 'none'; % Transparent background
hb.XAxis.Visible = 'off';
hb.YAxis.Visible = 'off';
% Move z-ruler to opposite corner (from undocumentedmatlab)
hb.ZAxis.FirstCrossoverValue = 1; % x-location of z-ruler [normalized]
hb.ZAxis.SecondCrossoverValue = 1; % y-location of z-ruler [normalized]
Note that this basic example breaks down when you start rotating the axes manually, or zooming in or out. You would need to add some methods that link the two axes together in order to take care of that.
The result would be:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With