Accessing and changing the Y-limits of multiple figures (2024)

16 views (last 30 days)

Show older comments

hxen on 10 Jan 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures

Commented: hxen on 10 Jan 2024

Accepted Answer: Matt J

Open in MATLAB Online

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

Accessing and changing the Y-limits of multiple figures (2)

Accessing and changing the Y-limits of multiple figures (3)

%% 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);

Accessing and changing the Y-limits of multiple figures (4)

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;

Accessing and changing the Y-limits of multiple figures (5)

% 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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Matt J on 10 Jan 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#answer_1386561

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#answer_1386561

Edited: Matt J on 10 Jan 2024

Open in MATLAB Online

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)])

Accessing and changing the Y-limits of multiple figures (7)

Accessing and changing the Y-limits of multiple figures (8)

Accessing and changing the Y-limits of multiple figures (9)

Accessing and changing the Y-limits of multiple figures (10)

3 Comments

Show 1 older commentHide 1 older comment

hxen on 10 Jan 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3023711

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3023711

Thanks Matt! I didn't know you could do something like: "ax(n)=gca().YAxis;" Thanks so much. This was very helpful. Happy New Year to you.

Steven Lord on 10 Jan 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3023721

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3023721

Open in MATLAB Online

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));

Accessing and changing the Y-limits of multiple figures (13)

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 on 10 Jan 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3024816

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2068276-accessing-and-changing-the-y-limits-of-multiple-figures#comment_3024816

Thank you as well Steven for this helpful suggestion.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations

Find more on Annotations in Help Center and File Exchange

Tags

  • ylim
  • yaxislocation

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Accessing and changing the Y-limits of multiple figures (15)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Accessing and changing the Y-limits of multiple figures (2024)

References

Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5839

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.