Problem with second axis in subplot
显示 更早的评论
Hi all! I plot in a loop several graphs in the same subplot. Now I need a second x axis in the subplot.
figure(1)
HandleP1 = subplot(2, 2, 1, 'Parent', p);
hold on;
plot(n, CH2VS)
ax1 = gca % current axes
ax1.Visible = 'off'; %just to see is the second axis is hidden anywhere
ac12 = axes('Position',ax1.Position,'XAxisLocation','top','YAxisLocation','right','Color','none');
ax12 = gca;
ax12.Visible = 'on';
ax12pos = ax1.Position;
ax12.XLim = ax1.XLim;
In my opinion, these few lines should do the trick, with the second x axis on top of the subplot. But the axis is simply not visible.
Whats wrong?
Best regards, Thomas
6 个评论
Stephen23
2017-10-20
Hi!
I made a test file, to address the problem. And I think I know the problem, but not the solution. When I leave the figure as it is, the second x axis on subplot 1 is will be shown. If I make a uipanel from the figure, it is not shown.
%%Test second x axis
clear all;
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
Fs = 10000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 5990; % Length of signal
t = (0:L-1)*T;
n = 1:5990;
%%Initialize figure
fig1 = figure(1);
p = uipanel('Parent',fig1,'BorderType','none');
p.Title = sprintf('Figure 1, No second x axis in subplot 1 visible');
p.TitlePosition = 'centertop';
p.FontSize = 12;
p.FontWeight = 'bold';
out = 200*rand(1,5990);
ax1 = subplot(2,2,1, 'Parent', p)
ax1_pos = ax1.Position; % position of first axes
ax12 = axes('Position',ax1.Position,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
ax12.XLim = [0, (L*T)];
ax12.XTick = 0:(1000*T):(L*T);
ax12.YTick = [];
%%figure 2
fig2 = figure(2);
out = 200*rand(1,5990);
ax1 = subplot(2,2,1);
ax1_pos = ax1.Position; % position of first axes
ax12 = axes('Position',ax1.Position,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
ax12.XLim = [0, (L*T)];
ax12.XTick = 0:(1000*T):(L*T);
ax12.YTick = [];
currentFigure = gcf;
supertitle = get(currentFigure, 'Children');
title(supertitle, 'Figure 2');
So, the correct question is: Why is the second axis in a subplot in an uipanel not shown?
Best, Thomas
" Why is the second axis in a subplot in an uipanel not shown?"
Have a look at how you are creating these subplots:
ax1 = subplot(2,2,1, 'Parent', p) % first subplot
...
ax1 = subplot(2,2,1); % second subplot
and the axes:
ax12 = axes(...) % but no Parent!
...
ax12 = axes(...) % but no Parent!
You never specify where to place the axes, and so by default they get placed in the figure, hidden by the uipanel.
I would suggest you should also rename the second subplot handle to ax2 (or in fact something more accurate, like sub2), because reusing the variable names is not helping you.
Thomas Schaefer
2017-10-20
Yes, because you do not specify the axes parent. You need to add 'Parent' to both axes calls: in the first case the figure, in the second case the subplot.
Without specifying the parent you simply create the axes in the current figure, which thus exist but are hidden by the uipanel.
For this reason I recommend that users always specify explicitly all graphics objects that they are using: because you can never assume that the current axes/figure/... is the one that you need to access. This means obtain the handle for every object that you need to access later, and then pass it correctly when required (e.g. by setting the parent property). This is a bit more work, but makes writing GUIs much easier once you get used to it.
Thomas Schaefer
2017-10-20
As I wrote before, you need to specify the axes parent. Your code does not specify the axes parent, and so the second axes are created in the figure rather than in the uipanel. And thus the second axes are perfectly hidden under the uipanel, because you made them exactly the same size!
You need to do this:
axes(p, ...) % specify the parent as the first input argument!
instead of this:
axes(...)
which you are doing now. Have a look at your code where you create the axes.
Every time you create or access a graphics object you should always specify the handles explicitly. Do NOT rely on the current axes/figure/etc. as being the correct object that you need!
You did not specify the axes parent, and so it defaults to using the current figure as its parent.
回答(1 个)
The MATLAB documentation shows how to add a second X-axis along the of the axes:
EDIT: in your case you need to remember to specify the 'Parent' of the axes that you create, otherwise the axes are created in the current figure.
类别
在 帮助中心 和 File Exchange 中查找有关 Subplots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!