Accessing and changing the Y-limits of multiple figures

25 views (last 30 days)
I have multiple figures of data with two y-axes. The left axis shows the histocounts of the values; the right axis gives the cummulative probability of distribution.
I cannot find a way to point to a figure handle and select the left-side ylim data and then reset the ylim values.
Goal: I wish to automatically determine the maximum ylim among the figures to then reset all the plots to the max ylim for easy comparison. The only working "solution" is using yyaxis to select "left" or 'right" and then accessing the ylim values. This however requires that the I manually use gcf after the analysis run and have the figure of interest at the top of the stack.
To summarize: I wish to get the max left-side ylims among a group of figures and then reset all the upper left-side ylims to the group maximum. To further help illustrate this issue, I have included sample data generating fake data for four data sets.
Thank you in advance any help.
datasets = 4;
for n = 1:datasets
data = randn(1,1000*n,4);
f(n) = figure; % fig handles
% plot histogram of random data
histogram(data(:,:,n));
ylabel('counts');
% overlay a cummulative distrib plot
hold on;
yyaxis right;
cdfplot(data(:,:,n));
ylabel('cummulative plot');
title(sprintf('phony balony data set #%s',num2str(n)));
end
%% trying to access the left side axis values
% (e.g. if trying to access fig f(3), hidden under f(4), assuming fig order untouched).
get(f(3).CurrentAxes);
ALim: [0 1] ALimMode: 'auto' AlphaScale: 'linear' Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 … ] (1×64 double) AmbientLightColor: [1 1 1] BeingDeleted: off Box: on BoxStyle: 'back' BusyAction: 'queue' ButtonDownFcn: '' CLim: [0 1] CLimMode: 'auto' CameraPosition: [-0.2000 0.5000 17.3205] CameraPositionMode: 'auto' CameraTarget: [-0.2000 0.5000 0] CameraTargetMode: 'auto' CameraUpVector: [0 1 0] CameraUpVectorMode: 'auto' CameraViewAngle: 6.6086 CameraViewAngleMode: 'auto' Children: [1×1 Line] Clipping: on ClippingStyle: 'rectangle' Color: [1 1 1] ColorOrder: [0.8500 0.3250 0.0980] ColorOrderIndex: 1 ColorScale: 'linear' Colormap: [256×3 double] ContextMenu: [0×0 GraphicsPlaceholder] CreateFcn: '' CurrentPoint: [2×3 double] DataAspectRatio: [7.9200 1 2] DataAspectRatioMode: 'auto' DeleteFcn: '' FontAngle: 'normal' FontName: 'Helvetica' FontSize: 10 FontSizeMode: 'auto' FontSmoothing: on FontUnits: 'points' FontWeight: 'normal' GridAlpha: 0.1500 GridAlphaMode: 'auto' GridColor: [0.1500 0.1500 0.1500] GridColorMode: 'auto' GridLineStyle: '-' GridLineWidth: 0.5000 GridLineWidthMode: 'auto' HandleVisibility: 'on' HitTest: on InnerPosition: [0.1300 0.1100 0.7750 0.8150] InteractionOptions: [1×1 matlab.graphics.interaction.interactionoptions.InteractionOptions] Interactions: [1×1 matlab.graphics.interaction.interface.DefaultAxesInteractionSet] Interruptible: on LabelFontSizeMultiplier: 1.1000 Layer: 'bottom' Layout: [0×0 matlab.ui.layout.LayoutOptions] Legend: [0×0 GraphicsPlaceholder] LineStyleCyclingMethod: 'aftercolor' LineStyleOrder: [7×2 char] LineStyleOrderIndex: 2 LineWidth: 0.5000 MinorGridAlpha: 0.2500 MinorGridAlphaMode: 'auto' MinorGridColor: [0.1000 0.1000 0.1000] MinorGridColorMode: 'auto' MinorGridLineStyle: ':' MinorGridLineWidth: 0.5000 MinorGridLineWidthMode: 'auto' NextPlot: 'add' NextSeriesIndex: 2 OuterPosition: [0 0 1 1] Parent: [1×1 Figure] PickableParts: 'visible' PlotBoxAspectRatio: [1 0.7897 0.7897] PlotBoxAspectRatioMode: 'auto' Position: [0.1300 0.1100 0.7750 0.8150] PositionConstraint: 'outerposition' Projection: 'orthographic' Selected: off SelectionHighlight: on SortMethod: 'childorder' Subtitle: [1×1 Text] SubtitleFontWeight: 'normal' Tag: '' TickDir: 'in' TickDirMode: 'auto' TickLabelInterpreter: 'tex' TickLength: [0.0100 0.0250] TightInset: [0.0964 0.0990 0.0877 0.0526] Title: [1×1 Text] TitleFontSizeMultiplier: 1.1000 TitleFontWeight: 'bold' TitleHorizontalAlignment: 'center' Toolbar: [1×1 AxesToolbar] Type: 'axes' Units: 'normalized' UserData: [] View: [0 90] Visible: on XAxis: [1×1 NumericRuler] XAxisLocation: 'bottom' XColor: [0.1500 0.1500 0.1500] XColorMode: 'auto' XDir: 'normal' XGrid: on XLabel: [1×1 Text] XLim: [-4.1600 3.7600] XLimMode: 'auto' XLimitMethod: 'tickaligned' XMinorGrid: off XMinorTick: off XScale: 'linear' XTick: [-4 -3 -2 -1 0 1 2 3] XTickLabel: {8×1 cell} XTickLabelMode: 'auto' XTickLabelRotation: 0 XTickLabelRotationMode: 'auto' XTickMode: 'auto' YAxis: [2×1 NumericRuler] YAxisLocation: 'right' YColor: [0.8500 0.3250 0.0980] YColorMode: 'manual' YDir: 'normal' YGrid: on YLabel: [1×1 Text] YLim: [0 1] YLimMode: 'auto' YLimitMethod: 'tickaligned' YMinorGrid: off YMinorTick: off YScale: 'linear' YTick: [0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1] YTickLabel: {11×1 cell} YTickLabelMode: 'auto' YTickLabelRotation: 0 YTickLabelRotationMode: 'auto' YTickMode: 'auto' ZAxis: [1×1 NumericRuler] ZColor: [0.1500 0.1500 0.1500] ZColorMode: 'auto' ZDir: 'normal' ZGrid: on ZLabel: [1×1 Text] ZLim: [-1 1] ZLimMode: 'auto' ZLimitMethod: 'tickaligned' ZMinorGrid: off ZMinorTick: off ZScale: 'linear' ZTick: [-1 0 1] ZTickLabel: '' ZTickLabelMode: 'auto' ZTickLabelRotation: 0 ZTickLabelRotationMode: 'auto' ZTickMode: 'auto'
ylim % will show me the [0 1] limits of the right axis
ans = 1×2
0 1
yyaxis left; % attempt to change back to left side axis;
% rechecking if accessed right axis
get(f(3).CurrentAxes);
ALim: [0 1] ALimMode: 'auto' AlphaScale: 'linear' Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 … ] (1×64 double) AmbientLightColor: [1 1 1] BeingDeleted: off Box: on BoxStyle: 'back' BusyAction: 'queue' ButtonDownFcn: '' CLim: [0 1] CLimMode: 'auto' CameraPosition: [-0.2000 0.5000 17.3205] CameraPositionMode: 'auto' CameraTarget: [-0.2000 0.5000 0] CameraTargetMode: 'auto' CameraUpVector: [0 1 0] CameraUpVectorMode: 'auto' CameraViewAngle: 6.6086 CameraViewAngleMode: 'auto' Children: [1×1 Line] Clipping: on ClippingStyle: 'rectangle' Color: [1 1 1] ColorOrder: [0.8500 0.3250 0.0980] ColorOrderIndex: 1 ColorScale: 'linear' Colormap: [256×3 double] ContextMenu: [0×0 GraphicsPlaceholder] CreateFcn: '' CurrentPoint: [2×3 double] DataAspectRatio: [7.9200 1 2] DataAspectRatioMode: 'auto' DeleteFcn: '' FontAngle: 'normal' FontName: 'Helvetica' FontSize: 10 FontSizeMode: 'auto' FontSmoothing: on FontUnits: 'points' FontWeight: 'normal' GridAlpha: 0.1500 GridAlphaMode: 'auto' GridColor: [0.1500 0.1500 0.1500] GridColorMode: 'auto' GridLineStyle: '-' GridLineWidth: 0.5000 GridLineWidthMode: 'auto' HandleVisibility: 'on' HitTest: on InnerPosition: [0.1300 0.1100 0.7750 0.8150] InteractionOptions: [1×1 matlab.graphics.interaction.interactionoptions.InteractionOptions] Interactions: [1×1 matlab.graphics.interaction.interface.DefaultAxesInteractionSet] Interruptible: on LabelFontSizeMultiplier: 1.1000 Layer: 'bottom' Layout: [0×0 matlab.ui.layout.LayoutOptions] Legend: [0×0 GraphicsPlaceholder] LineStyleCyclingMethod: 'aftercolor' LineStyleOrder: [7×2 char] LineStyleOrderIndex: 2 LineWidth: 0.5000 MinorGridAlpha: 0.2500 MinorGridAlphaMode: 'auto' MinorGridColor: [0.1000 0.1000 0.1000] MinorGridColorMode: 'auto' MinorGridLineStyle: ':' MinorGridLineWidth: 0.5000 MinorGridLineWidthMode: 'auto' NextPlot: 'add' NextSeriesIndex: 2 OuterPosition: [0 0 1 1] Parent: [1×1 Figure] PickableParts: 'visible' PlotBoxAspectRatio: [1 0.7897 0.7897] PlotBoxAspectRatioMode: 'auto' Position: [0.1300 0.1100 0.7750 0.8150] PositionConstraint: 'outerposition' Projection: 'orthographic' Selected: off SelectionHighlight: on SortMethod: 'childorder' Subtitle: [1×1 Text] SubtitleFontWeight: 'normal' Tag: '' TickDir: 'in' TickDirMode: 'auto' TickLabelInterpreter: 'tex' TickLength: [0.0100 0.0250] TightInset: [0.0964 0.0990 0.0877 0.0526] Title: [1×1 Text] TitleFontSizeMultiplier: 1.1000 TitleFontWeight: 'bold' TitleHorizontalAlignment: 'center' Toolbar: [1×1 AxesToolbar] Type: 'axes' Units: 'normalized' UserData: [] View: [0 90] Visible: on XAxis: [1×1 NumericRuler] XAxisLocation: 'bottom' XColor: [0.1500 0.1500 0.1500] XColorMode: 'auto' XDir: 'normal' XGrid: on XLabel: [1×1 Text] XLim: [-4.1600 3.7600] XLimMode: 'auto' XLimitMethod: 'tickaligned' XMinorGrid: off XMinorTick: off XScale: 'linear' XTick: [-4 -3 -2 -1 0 1 2 3] XTickLabel: {8×1 cell} XTickLabelMode: 'auto' XTickLabelRotation: 0 XTickLabelRotationMode: 'auto' XTickMode: 'auto' YAxis: [2×1 NumericRuler] YAxisLocation: 'right' YColor: [0.8500 0.3250 0.0980] YColorMode: 'manual' YDir: 'normal' YGrid: on YLabel: [1×1 Text] YLim: [0 1] YLimMode: 'auto' YLimitMethod: 'tickaligned' YMinorGrid: off YMinorTick: off YScale: 'linear' YTick: [0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1] YTickLabel: {11×1 cell} YTickLabelMode: 'auto' YTickLabelRotation: 0 YTickLabelRotationMode: 'auto' YTickMode: 'auto' ZAxis: [1×1 NumericRuler] ZColor: [0.1500 0.1500 0.1500] ZColorMode: 'auto' ZDir: 'normal' ZGrid: on ZLabel: [1×1 Text] ZLim: [-1 1] ZLimMode: 'auto' ZLimitMethod: 'tickaligned' ZMinorGrid: off ZMinorTick: off ZScale: 'linear' ZTick: [-1 0 1] ZTickLabel: '' ZTickLabelMode: 'auto' ZTickLabelRotation: 0 ZTickLabelRotationMode: 'auto' ZTickMode: 'auto'
ylim % will show me the ylim range for f(4) that is opened, not f(3)!
ans = 1×2
0 400

Accepted Answer

Matt J
Matt J on 10 Jan 2024
Edited: Matt J on 10 Jan 2024
datasets = 4;
clear ax
for n = 1:datasets
data = randn(1,1000*n,4);
f(n) = figure; % fig handles
% plot histogram of random data
histogram(data(:,:,n));
ylabel('counts');
ax(n)=gca().YAxis;
% overlay a cummulative distrib plot
hold on;
yyaxis right;
cdfplot(data(:,:,n));
ylabel('cummulative plot');
title(sprintf('phony balony data set #%s',num2str(n)));
end
ymax=max(vertcat(ax.Limits));
set(ax,'Limits',[0,ymax(2)])
  3 Comments
Steven Lord
Steven Lord on 10 Jan 2024
Rather than using gca here, I'd store the histogram handle by calling it with an output then ask for the axes ancestor of that histogram. That way you don't have to assume that the curent axes is what you think it is (that no one has clicked on a different axes, for example.)
h = histogram(randn(1, 1e5));
ax = ancestor(h, 'axes')
ax =
Axes with properties: XLim: [-5.0450 4.7450] YLim: [0 4500] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Use GET to show all properties
hxen
hxen on 10 Jan 2024
Thank you as well Steven for this helpful suggestion.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!