Create a figure with the same x-axis on top and bottom, and y-axis on the left and right.

8 views (last 30 days)
Ethan Yen on 4 Feb 2023
Answered: the cyclist on 5 Feb 2023
How can I create a figure with the same x-axis on top and bottom of my figure. Also, how can I do the same with the same y-axis on the right and left? Also, how can I have labels for the top,bottom, left, and right of the figure.

the cyclist on 4 Feb 2023
Edited: the cyclist on 4 Feb 2023
I am not entirely sure I understand everything you want. But, borrowing from this answer, here is code that will label ticks on all sides of a plot. Maybe you can adapt it to what you need.
% Plot some data
figure
plot(1:10);
% First, store the handle to those axes.
% Next create a second set of axes,
% position This on top of the first and make it transparent.
ax1=gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
% set the same Limits and Ticks on ax2 as on ax1;
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'XTick', get(ax1, 'XTick'), 'YTick', get(ax1, 'YTick'));
OppXTickLabels = {'a' 'b' 'c' 'd' 'e' 'f'};
OppYTickLabels = {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k'};
% Set the x-tick and y-tick labels for the second axes
set(ax2, 'XTickLabel', OppXTickLabels,'YTickLabel',OppYTickLabels);
I think you might also find the hold function useful (if you meant that you want two plots on one set of axes), and also the yyaxis funciton might have been useful.
Ethan Yen on 4 Feb 2023
Thanks for answering my question! I wasn't very clear with my previous question. I have this sample histogram that I have created. I am trying to get the y-axis on the right side to match the y-axis on the left side. Also, I am trying to do the same with the x-axis. I am trying to figure out how I can have the x-axis on the top of the figure be the same as the x-axis at the bottom of the figure.

the cyclist on 5 Feb 2023
Based on your comment on my other solution, here is a different guess as to what you mean. (I'm still not really sure.)
This code uses the tiledlayout function to make three plots side-by-side (in this case some random histograms), and then uses the linkaxes function to synchronize the x- and y-axes.
figure
tiledlayout(1,3)
% Tile 1
ax1 = nexttile;
histogram(randn(1000,1)+2)
% Tile 2
ax2 = nexttile;
histogram(randn(1000,1)+7)
% Tile 3
ax3 = nexttile;
histogram(randn(1000,1)+19)