How can I plot a figure with 3 y-axis and and common logarithmic x-axis?
159 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2024-1-8
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
采纳的回答
Hassaan
2024-1-8
% Sample data
x = logspace(0, 2, 100); % Logarithmic x-axis data
y1 = sin(x);
y2 = cos(x);
y3 = exp(0.05 * x);
% Ensure y1 is positive and non-zero for the loglog plot
y1_positive = abs(y1) + 1e-6; % Adding a small constant to avoid log of zero
% Create figure
figure;
% Plot the first dataset with a logarithmic x-axis
yyaxis left;
loglog(x, y1_positive, 'b-', 'LineWidth', 1); % Use 'loglog' for logarithmic x and y axes
ylabel('Left Y-axis (sin(x))');
% Add a second y-axis on the right and plot the second dataset
yyaxis right;
semilogx(x, y2, 'r-', 'LineWidth', 1); % Use 'semilogx' for logarithmic x-axis only
ylabel('Right Y-axis 1 (cos(x))');
% Manually create a third y-axis
ax1 = gca; % Current axes
ax2 = axes('Position', ax1.Position, ...
'XAxisLocation', 'top', ...
'YAxisLocation', 'right', ...
'Color', 'none', ...
'XColor', 'k', 'YColor', 'm', ...
'XScale', 'log', ... % Set x-axis to logarithmic scale
'YTick', [], ... % Hide y-ticks on the third axis
'XTick', []); % Hide x-ticks on the third axis
linkaxes([ax1, ax2], 'x'); % Link the x-axes
% Plot the third dataset on the third y-axis
line(x, y3, 'Parent', ax2, 'Color', 'm', 'LineWidth', 1);
ylabel(ax2, 'Right Y-axis 2 (exp(0.05x))');
% Label the shared x-axis
xlabel('Logarithmic X-axis');
% Ensure the plot box is on and grid is visible
box on;
grid on;
% Set the limits of the x-axis (optional)
xlim([min(x), max(x)]);
% Add a title and a grid
title('Multi-axis Plot with Logarithmic X-axis');
grid on;
- y1 (sin(x)) on the left y-axis using a loglog plot (logarithmic in both x and y). Negative values are handled by taking the absolute value and adding a small offset to avoid the logarithm of zero.
- y2 (cos(x)) on the right y-axis using a semilogx plot (logarithmic in x only).
- y3 (exp(0.05 * x)) on an additional manually created right y-axis also using a logarithmic x-axis.
Remember to replace the y1, y2, and y3 datasets with your actual data. Adjust the xlim and other plot settings as needed to suit your specific requirements.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
0 个评论
更多回答(1 个)
Ayush
2024-1-8
编辑:Ayush
2024-1-8
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!