Plotting multiple y axes on one side only using addaxis

186 visualizzazioni (ultimi 30 giorni)
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?
If I do this:
figure
plot(x1,y1);
addaxis(x2,y2);
set(gca,'yaxislocation','right');
I get the axes on top of each other.
Thanks
  2 Commenti
Adam Danz
Adam Danz il 1 Mag 2020
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.
Dirk
Dirk il 2 Mag 2020
I want both axes on the right, but offset from each other.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 2 Mag 2020
Modificato: Adam Danz il 4 Mag 2020
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);
  5 Commenti
Peter Sakkos
Peter Sakkos il 17 Dic 2020
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
Adam Danz il 17 Dic 2020
I suggest starting a new question since this is off topic from the original question in this thread.

Accedi per commentare.

Più risposte (1)

Tommy
Tommy il 1 Mag 2020
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 Commenti
Dirk
Dirk il 4 Mag 2020
Thanks for the update, it seems to work OK, but I get some better control with the second option.Many thanks again, much appreciated
Tommy
Tommy il 4 Mag 2020
You're welcome. I completely agree with Adam's advice.

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by