Plotting multiple y axes on one side only using addaxis
136 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am using addaxis to plot multiple y axes (first pic), but is there a way to have them all appear on one side (say the right hand side). Is there a better function to do this than addaxis?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289209/image.png)
If I do this:
figure
plot(x1,y1);
addaxis(x2,y2);
set(gca,'yaxislocation','right');
I get the axes on top of each other.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289210/image.png)
Thanks
2 件のコメント
Adam Danz
2020 年 5 月 1 日
So you want two y axis with different scales, both on the right side? What would the tick labels look like? It sounds like a visual nightmare if I'm understanding it correctly.
採用された回答
Adam Danz
2020 年 5 月 2 日
編集済み: Adam Danz
2020 年 5 月 4 日
I briefly looked at the addaxis function on the file exchange and I'm not sure it's using the best approach.
Follow this demo below to produce the figure at the bottom. I suggest stepping through the code line by line and reading the comments so you can understand what each line is doing. The order of what's happening is important. If some of the axis alterations are made before plotting the data, the alterations will not be implemented correctly.
The most important part is scaling the axis ticks from the 2nd axis so they align with the axis ticks of the 1st axes. Otherwise you'll have different sets of tick locations which makes the plot very difficult to read.
% Create two identical overlaying axes
fig = figure();
ax1 = axes();
ax2 = copyobj(ax1, fig);
% Link the x axes
linkaxes([ax1,ax2],'x')
% Choose colors for the 2 y axes.
axColors = [
0 0.39063 0; % dark green
0.53906 0.16797 0.88281]; % blue violet
% Do plotting (we'll adjust the axes and the ticks later)
plot(ax1, 1:100, sort(randn(1,100)*100), '-', 'Color', axColors(1,:));
plot(ax2, 1:100, sort(randn(1,100)*60), '-', 'Color', axColors(2,:));
% Set axes background color to transparent and turns box on
set([ax1,ax2],'Color', 'None', 'Box', 'on')
% Set y-ax to right side
set([ax1,ax2], 'YAxisLocation', 'right')
% make the axes 10% more narrow
set([ax1,ax2], 'Position', ax1.Position .* [1 1 .90 1])
% set the y-axis colors
ax1.YAxis.Color = axColors(1,:);
ax2.YAxis.Color = axColors(2,:);
% After plotting set the axis limits.
ylim(ax1, ylim(ax1));
ylim(ax2, ylim(ax2));
% Set the y-ticks of ax1 so they align with yticks of ax2
% This also rounds the ax1 ticks to the nearest tenth.
ax1.YTick = round(linspace(min(ax1.YTick),max(ax1.YTick),numel(ax2.YTick)),1);
% Extend the ax1 y-ax ticklabels rightward a bit by adding space
% to the left of the label. YTick should be set first.
ax1.YTickLabel = strcat({' '},ax1.YTickLabel);
% Set y-ax labels and adjust their position (5% inward)
ylabel(ax1,'y axis 1')
ylabel(ax2','y axis 2')
ax1.YLabel.Position(1) = ax1.YLabel.Position(1) - (ax1.YLabel.Position(1)*.05);
ax2.YLabel.Position(1) = ax2.YLabel.Position(1) - (ax2.YLabel.Position(1)*.05);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289715/image.png)
5 件のコメント
Peter Sakkos
2020 年 12 月 17 日
Here is the code I use, where vel, acc, and altitude are arrays of data not shown here.
figure(2)
vel = v_entry.*exp(-1/(2*zeta).*exp(-bz));
plot(vel, altitude/1000, 'LineWidth', 4, 'Color', [0.2, 0.6470, 0.810]);
set(gca, "FontName","Comic Sans MS", "FontSize", 16);
ylabel("Altitude (km)"); xlabel('Velocity (m/s)');
yticks(0:5:55); ylim([0 55]); xticks(0:v_entry/6:v_entry); xlim([0 v_entry]);
ax1 = gca;
hold on
ax1_pos = ax1.Position;
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','bottom',...
'Color','none')
hold(ax2,'on')
set(ax2,'YTick',([]));
plot(ax2, acc, altitude./1000,'LineWidth', 4, 'Color', [0.6, 0.4470, 0.7410]);
yticks(0:5:55); ylim([0 55]); xticks(0:450/6:450); xlim([0 450]);
set(gca, "FontName","Comic Sans MS", "FontSize", 16);
title("Altitude vs Hypersonic Re-Entry Velocity");
xlabel("Acceleration Normalized with Gravity (g)");
Adam Danz
2020 年 12 月 17 日
I suggest starting a new question since this is off topic from the original question in this thread.
その他の回答 (1 件)
Tommy
2020 年 5 月 1 日
It looks like that function flips back and forth between adding the new axes to the left and adding them to the right. How about calling the function three times and deleting the second set of axes after the third has been added?
plot(1:10, rand(10,1));
addaxis(NaN,NaN);
addaxis(1:10, rand(10,1));
axh = getaddaxisdata(gca,'axisdata');
delete(axh{1})
4 件のコメント
参考
カテゴリ
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!