Plotting multiple y axes on one side only using addaxis

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 Kommentare

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.
I want both axes on the right, but offset from each other.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Adam Danz
Adam Danz am 2 Mai 2020
Bearbeitet: Adam Danz am 4 Mai 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 Kommentare

Do you know how to do this instead with multiple x-axes?
Peter Sakkos
Peter Sakkos am 17 Dez. 2020
Bearbeitet: Peter Sakkos am 17 Dez. 2020
Okay, as of now, am having a series of issues:
I am trying to plot two sets of data (1 is in terms of acceleration and the other is in terms of velocity) w/ respect to a single y axis that is in terms of altitude.
1) When I add my axes, they overlap each other. I don't really know how to space out the x axes ticks and labels without having them cutoff from view.
2) I only want to show 1 set of y axis values, but it appears that two values are being plotted. How would I remove one of them?
3) For some reason, when I resize the graph, the two seperate axis plots are not in alighment (they are aligned when the graph is initially created). What can I do to keep the graphs aligned?
Here is my plot, for reference.
I want something that looks similar to the very first plot posted at the following link.
https://www.mathworks.com/matlabcentral/answers/571378-multiple-x-axis-in-different-scale-but-one-y-axis#answer_471847
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)");
I suggest starting a new question since this is off topic from the original question in this thread.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Tommy
Tommy am 1 Mai 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 Kommentare

Thanks for that. It works if the axes are to be kept on the left side, but not sure how to make it work if I want all the axes on the right hand side. Cheers
Happy to help!
Try this:
plot(NaN,NaN);
set(gca, 'YColor', 'none');
addaxis(1:10, rand(10,1));
addaxis(NaN,NaN);
addaxis(1:10, rand(10,1));
axh = getaddaxisdata(gca,'axisdata');
delete(axh{2})
Thanks for the update, it seems to work OK, but I get some better control with the second option.Many thanks again, much appreciated
You're welcome. I completely agree with Adam's advice.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019a

Gefragt:

am 1 Mai 2020

Kommentiert:

am 17 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by