Subplot problem - plots are on top of each other?

27 views (last 30 days)
I have a script that creates 2 plots. I would like them to be next to each other in a typical subplot form. However, when I run this script, it only plots the second plot and the axes are messed up, such that it looks like it's layering plots on top of each other perhaps. If I take out the subplot syntax and run them as separate figures, they look fine. It's only and issue when I try to use subplot. I cannot figure out what the problem is. I know my code isn't going to win any code beauty pagents, but this is an odd problem I cannot figure out. I'm running 2018a. Thank you all in advance.
%%%Plot Audiograms for SAL
%%%Plot for NH group
%Get NH means from data
IowaNHR_Mean = mean(AudioData.Iowa.NHR);
IowaNHL_Mean = mean(AudioData.Iowa.NHL);
CalNHR_Mean = mean(AudioData.Cal.NHR);
CalNHL_Mean = mean(AudioData.Cal.NHL);
%plot NH group
subplot(1,2,1);
h1 = axes;
plot(IowaNHR_Mean,'r','linewidth',2);
set(h1, 'Ydir', 'reverse');
xlim([0.5 6.5]);
ylim([-20 120]);
set(gca, 'XTick' ,[1 2 3 4 5 6]);
set(gca, 'XTickLabel',{'0.25'; '0.5'; '1'; '2'; '4'; '8'});
grid on
title('Normal Hearing', 'fontsize',25');
ylabel('Threshold (dB HL)', 'fontsize', 20);
xlabel('Frequency (kHz)', 'fontsize', 20);
hold on;
%%Plot Means
plot(IowaNHL_Mean,'b','linewidth',2);
hold on;
plot(CalNHR_Mean, 'r', 'linestyle','--','linewidth',2);
hold on;
plot(CalNHL_Mean, 'b', 'linestyle','--','linewidth',2);
hold on;
%Plot Individual Data
x = 1:6;
y = AudioData.Iowa.NHR;
plot(x,y, 'color', [0,0,0]+0.6);
hold all;
y2 = AudioData.Iowa.NHL;
plot(x,y2, 'color', [0,0,0]+0.6);
hold all;
y3 = AudioData.Cal.NHR;
plot(x,y3,'color',[0,0,0]+0.6, 'linestyle','--');
hold all;
y4 = AudioData.Cal.NHL;
plot(x,y4,'color',[0,0,0]+0.6, 'linestyle','--');
%%Legend
legend('Rural right ear mean','Rural left ear mean','Urban right ear mean','Urban left ear mean','Location','southwest');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Plot for HL group
%Get HL means fro mdata
IowaHLR_Mean = mean(AudioData.Iowa.HLR);
IowaHLL_Mean = mean(AudioData.Iowa.HLL);
CalHLR_Mean = mean(AudioData.Cal.HLR);
CalHLL_Mean = mean(AudioData.Cal.HLL);
%plot HL group
subplot(1,2,2)
h2 = axes;
plot(IowaHLR_Mean,'r','linewidth',2);
set(h2, 'Ydir', 'reverse');
xlim([0.5 6.5]);
ylim([-20 120]);
set(gca, 'XTick' ,[1 2 3 4 5 6]);
set(gca, 'XTickLabel',{'0.25'; '0.5'; '1'; '2'; '4'; '8'});
grid on
title('Hearing Loss', 'fontsize',25');
ylabel('Threshold (dB HL)', 'fontsize', 20);
xlabel('Frequency (kHz)', 'fontsize', 20);
hold on;
%plot means
plot(IowaHLL_Mean,'b','linewidth',2);
hold on;
plot(CalHLR_Mean, 'r', 'linestyle','--','linewidth',2);
hold on;
plot(CalHLL_Mean, 'b', 'linestyle','--','linewidth',2);
hold on;
%plot individual data
x = 1:6;
y = AudioData.Iowa.HLR;
plot(x,y, 'color', [0,0,0]+0.6);
hold all;
y2 = AudioData.Iowa.HLL;
plot(x,y2, 'color', [0,0,0]+0.6);
hold all;
y3 = AudioData.Cal.HLR;
plot(x,y3,'color',[0,0,0]+0.6, 'linestyle','--');
hold all;
y4 = AudioData.Cal.HLL;
plot(x,y4,'color',[0,0,0]+0.6, 'linestyle','--');
%%Legend
legend('Rural right ear mean','Rural left ear mean','Urban right ear mean','Urban left ear mean','Location','southwest');

Accepted Answer

Walter Roberson
Walter Roberson on 16 Oct 2019
subplot(1,2,1);
h1 = axes;
The subplot() call creates an axes that covers half of the figure. The axes() call creates an axes in the default position, covering the figure. These are not the same axes. You should use
h1 = subplot(1,2,1);
  1 Comment
Erik J
Erik J on 16 Oct 2019
Thank you so much! I figured it was something simple. I really appreciate it.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!