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

36 次查看(过去 30 天)
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.

回答(2 个)

the cyclist
the cyclist 2023-2-4
编辑:the cyclist 2023-2-4
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.
  1 个评论
Ethan Yen
Ethan Yen 2023-2-4
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
the cyclist 2023-2-5
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)
% Link the axes
linkaxes([ax1, ax2, ax3])

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by