How can I plot a figure with 3 y-axis and and common logarithmic x-axis?
Afficher commentaires plus anciens
In short I have three different graphs that I need to plot to the same figure but with 3 different y-axis. Inbuilt plotyy.m plots two y-axis just fine and multiplotyyy.m for example handles three y-axis well. Problem however araises when I try to get these functions to work with logaritmic x-axis. How can this be done?
1 commentaire
Dyuman Joshi
le 8 Jan 2024
What is the expected output?
I assume you are using this FEX submission - https://in.mathworks.com/matlabcentral/fileexchange/39595-multiplotyyy?s_tid=ta_fx_results
Réponse acceptée
Plus de réponses (1)
My understanding of your question is that you need 3-axes and x-axis in log scale. Here is the conceptual code for that:
% Example data
x = logspace(-1, 1, 100); % Logarithmic x-axis data
y1 = sin(x);
y2 = cos(x);
y3 = exp(x);
% Create the first two y-axes using plotyy
[ax, h1, h2] = plotyy(x, y1, x, y2, 'semilogx');
% Set properties of the first two axes
set(ax(1), 'XScale', 'log', 'XColor', 'k', 'YColor', 'b');
set(ax(2), 'XScale', 'log', 'XColor', 'k', 'YColor', 'r');
ylabel(ax(1), 'First Y-axis');
ylabel(ax(2), 'Second Y-axis');
% Add the third axis
ax3 = axes('Position', ax(2).Position, 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'Color', 'none', 'XColor', 'k', 'YColor', 'g');
linkaxes([ax(1), ax3], 'x'); % Link the x-axes
% Offset the third axis from the second
offset = 1; % Adjust this value as needed
ax3_pos = get(ax3, 'Position');
set(ax3, 'Position', [ax3_pos(1)+ax3_pos(3)*offset ax3_pos(2) ax3_pos(3)*(1-offset) ax3_pos(4)]);
set(ax3, 'box', 'off'); % Turn off the box to avoid drawing the top spine
% Plot the third data set on the third axis
h3 = line(x, y3, 'Color', 'g', 'Parent', ax3);
ylabel(ax3, 'Third Y-axis');
set(ax3, 'XScale', 'log', 'YAxisLocation', 'right', 'XTick', []);
% Adjust the axes limits if necessary
set(ax3, 'YLim', [min(y3), max(y3)]);
set(ax3, 'XLim', get(ax(1), 'XLim')); % Ensure the x-axis limits match
% Add legends
legend([h1, h2, h3], {'Data1', 'Data2', 'Data3'});
Thanks,
Ayush
Catégories
En savoir plus sur Images dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

