Plot a composite function with two y axis, with one of them scaling according to a function

2 views (last 30 days)
Hi all,
I want to plot a composite function on a Matlab plot with two y axes. I have 3 variables: position, intensity and temperature. Position is the x one, on the horizontal axis. Intensity and temperature have to go on the y axes. However, intensity and temperature are related to each other by a logarithmic function, for which I know the equation. If I use yyaxis left and yyaxis right, I obviously get that x and y scale linear, both on the left and on the right, so I get two plots that don't superimpose. Is there a way to scale the right axis according to the known relationship between intensity and temperature, so that in the end I get only one plot?
Thanks!
for i=1:5
yyaxis left
h1=plot(pos_S1(:,i),intensity_S1(:,i),'o','color',colors(i,:));
set(h1, 'markerfacecolor', get(h1, 'color'),'markersize',2);
hold on
ylabel('Normalized intensity [-]');
yyaxis right
h2=plot(pos_S1(:,i),temperature_S1(:,i),'o','color',colors(i,:));
set(h2, 'markerfacecolor', get(h1, 'color'),'markersize',2);
hold on
set(gca, 'Ydir', 'reverse')
ylabel('Temperature [\circC]');
xlabel('Position [\mum]','FontSize',16)
end

Answers (1)

Chetan
Chetan on 8 May 2024
I understand you're aiming to plot intensity and temperature against position on a MATLAB plot featuring two y-axes.
To accomplish this, plot the intensity on the left y-axis and manually adjust the right y-axis (temperature) to mirror the logarithmic relationship with intensity.
This step involves customizing tick marks and labels on the right y-axis post-plotting.
Here is the sample code for the same:
% Example of plotting and adjusting y-axis scales
yyaxis left
plot(pos_S1(:,1), intensity_S1(:,1), 'o'); % Plotting intensity
ylabel('Intensity');
yyaxis right
plot(pos_S1(:,1), temperature_S1(:,1), 'o'); % Plotting temperature
ylabel('Temperature');
% Adjusting the right y-axis to align with the logarithmic relationship
% Calculating corresponding temperatures for a range of intensities
intensity_range = linspace(min(intensity_S1(:)), max(intensity_S1(:)), 10);
temperature_ticks = log(intensity_range); % Using a simple log relationship for illustration
% Applying calculated temperatures as tick labels (adjust according to your relationship)
set(gca, 'YTick', temperature_ticks, 'YTickLabel', arrayfun(@num2str, temperature_ticks, ...
'UniformOutput', false));
Please ensure to modify the `log(intensity_range)` part to accurately reflect your specific logarithmic relationship between intensity and temperature.
Refer to the following MathWorks Documentation for more details:
Thanks
Chetan

Categories

Find more on Graphics in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!