# Multiple axes in a subplot

92 Ansichten (letzte 30 Tage)
Adam Fitchett am 18 Okt. 2021
Bearbeitet: dpb am 19 Okt. 2021
Hello, how do I create multiple axes within subplots? I want each plot in the subplot to have two y axes and two x axes
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Scott MacKenzie am 18 Okt. 2021
I don't believe you can create a plot with two x-axes. To create a plot with two y-axes, use yyaxis left followed by plot for the left-axis plot, then yyaxis right before plot for the the right-axis plot. Type doc yyaxis for examples.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Kelly Kearney am 18 Okt. 2021
You can achieve the multiple-axis look by layering different axes on top of each other. There are several File Exchange entries that might help (e.g. offsetaxis).
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

dpb am 18 Okt. 2021
Bearbeitet: dpb am 19 Okt. 2021
It's a pain -- MathWorks doesn't think you're supposed to ever want to do that; there are no builtin tools to do it. There may be some tools at the FEX that help, but I've not looked recently.
It's doable, but unfortunately, none of the newer stuff that has been introduced helps -- one has to stick with the venerable subplot because there is only one underlying x axes with yyaxis and nextplot and tiled axes will only allow one axis.
Following is a basic outline of the sequence you have to follow -- it is convoluted and highly error-prone, but can be made to work if you have enough perserverence and patience. (This isn't the only way; see my follow-up note on this particular way...)
figure
hAx=subplot(2,1,1); % create the first subplot axes
hAx.Position=hAx.Position.*[1 1 1 0.8]; % shorten height for 2nd x axis room
hAx=plotyy(hAx,1:10,rand(1,10),1:10,randn(1,10)); % and the second; keep array of handles
hAx(1,2).XAxisLocation='top';
hAx(1,2).XAxis.Visible='on';
hAx(2,1)=subplot(2,1,2);
hAx(2,:)=plotyy(hAx(2,1),1:10,rand(1,10),1:10,randn(1,10));
hAx(2,2).XAxisLocation='top';
hAx(2,2).XAxis.Visible='on';
xlabel(hAx(1,2),'XLabel 2')
xlabel(hAx(2,2),'XLabel 2')
produced
The above could be turned into a more generalized routine but I've never taken the time to do so...but it gives the outline of the needed machinations to get to the second axes created by plotyy
NB: the need to keep a 2D array of axes handles by subplot and by axes within each subplot. It's really easy to mung up one of those and lose access.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
dpb am 18 Okt. 2021
Bearbeitet: dpb am 19 Okt. 2021
NB First: You can create the second axes as empty axes with plotyy by passing NaN as the X,Y data arrays.
NB Second: You can, of course, also go about it by creating the second axes object from scratch with the .Position retrieved from the first and setting all the other needed properties. I typically do as above and let plotyy handle all that except that it hides the second X axes, but it is created; you just have to make it visible and put it at the top. Saves a half-dozen steps or so explicit steps...

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Two y-axis finden Sie in Help Center und File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by