Change scale of second y-axis on figure

1 view (last 30 days)
Eric Escoto
Eric Escoto on 29 Sep 2020
Answered: Cris LaPierre on 30 Sep 2020
Hello,
I have a figure with two axis, one of which is inverted. The inverted axis needs to be compressed in the y direction so the bars do not ovelap.
Here's my current code, and the figure result is attached.
%% Plot of summary table vs month/day AVERAGES for study period (Jul 2019 to Sep 2020) SUMMER
fig1 = figure; % Summer Precip
hAxes(1) = subplot(2, 1, 1);
yyaxis left
bar(1:height(P2SFL_P_DSsummerperiod_summarytable),P2SFL_P_DSsummerperiod_summarytable.mean_Var1, 'FaceColor', 'blue')
axis ij
ylabel('[mm]')
ylim([0 10])
hold on
yyaxis right
bar(1:height(P2SFL_KE_DSsummerperiod_summarytable),P2SFL_KE_DSsummerperiod_summarytable.mean_Var1)%, 'FaceColor', 'black')
% Decide how many ticks you want. This does every 10 rows (10 days if none missing)
tick_idx = 1:7:height(P2SFL_KE_DSsummerperiod_summarytable);
xticks(tick_idx)
% Label ticks with month and day (e.g., "July 1")
xticklabels(string(P2SFL_KE_DSsummerperiod_summarytable.Month(tick_idx))+" "+P2SFL_KE_DSsummerperiod_summarytable.Day(tick_idx))
% Angle ticks at 45 degrees for easier reading when closely packed
xtickangle(45)
title('CBBG-SFL, Mesa, AZ')%, 'FontSize', 15)
ylabel('[J/m^2]')
%ylim([0 15])
%xlabel('Month and Day')
hAxes(2) = subplot(2, 1, 2);
yyaxis left
bar(1:height(P2JER_P_DSsummerperiod_summarytable),P2JER_P_DSsummerperiod_summarytable.mean_Var1, 'FaceColor', 'blue')
axis ij
ylabel('[mm]')
hold on
yyaxis right
bar(1:height(P2JER_KE_DSsummerperiod_summarytable),P2JER_KE_DSsummerperiod_summarytable.mean_Var1) %, 'FaceColor', 'black')
% Decide how many ticks you want. This does every 10 rows (10 days if none missing)
tick_idx = 1:7:height(P2JER_KE_DSsummerperiod_summarytable);
xticks(tick_idx)
% Label ticks with month and day (e.g., "July 1")
xticklabels(string(P2SFL_KE_DSsummerperiod_summarytable.Month(tick_idx))+" "+P2SFL_KE_DSsummerperiod_summarytable.Day(tick_idx))
% Angle ticks at 45 degrees for easier reading when closely packed
xtickangle(45)
%set(gca,'xticklabel',{[]})
title('JER, Las Cruces, NM')%, 'FontSize', 15)
ylabel('[J/m^2]')
%ylim([20 30])
% Give common xlabel, ylabel and title to your figure
linkaxes(hAxes, 'x');
%xlim([1 92])
han=axes(fig1,'visible','off');
han.Title.Visible='on';
han.XLabel.Visible='on';
han.YLabel.Visible='off';
%xlabel(han,'Month and Day');
%title(han,'Las Cruces International Airport, Summer Season (2001-2020)');
sgtitle('Daily Average Precipitation and Kinetic Energy, Summer Season (2019-2020)')
Thanks!

Answers (1)

Cris LaPierre
Cris LaPierre on 30 Sep 2020
Use the ylim function to set the yaxis limits.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!