How to plot to two figures simultaneously? (2024)

165 views (last 30 days)

Show older comments

Hugh Wheaton on 20 Apr 2020

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously

Commented: Adam Danz on 23 Apr 2020

Accepted Answer: Adam Danz

Here's the idea:

I'm making a live script to be printed and submitted for uni. At one point, I have to plot like 20 figures. It is more convenient to plot all these on a 4x5 subplot to be quickly looked over for mistakes etc., but this isn't good for detail. I was hoping to then simultaneously plot to an invisible figure (say fig(m,"...") in a for loop m=1:20), while also plotting to this subplot, and then printing the resultant figures to (hopefully one multi-page) pdf documents. I'd then attach this to my assignment for further scrutiny.

But nothing seems to do this, and everything I search is saturated with subplot faqs.

Is this reasonable or no? I get atm I could just duplicate the code, but that kinda destroys the whole idea of simultanous coding as it will get really messy, and get quite slow, very quickly.

1 Comment

Show -1 older commentsHide -1 older comments

Michael Soskind on 20 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_831535

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_831535

Open in MATLAB Online

Hi Hugh,

Seemingly, you would like to plot to two figures, is there a reason you have not tried to do the following?

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Adam Danz on 20 Apr 2020

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#answer_427119

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#answer_427119

Edited: Adam Danz on 22 Apr 2020

Open in MATLAB Online

Create the subplots and then copy each subplot axes to an independent figure at full size.

Here's a demo.

% Create subplot

figure()

sp(1) = subplot(2,2,1);

plot(rand(20,2), '-o')

sp(2) = subplot(2,2,2);

surf(peaks(15))

sp(3) = subplot(2,2,3);

[X,Y] = meshgrid(linspace(-2*pi,2*pi),linspace(0,4*pi));

contour(X,Y,sin(X)+cos(Y))

sp(4) = subplot(2,2,4);

histogram(randn(1,100)+10);

% copy each subplot to an independent figure

for i = 1:numel(sp)

newfig = figure();

axCopy = copyobj(sp(i),newfig);

axCopy.Position = [0.13 0.11 0.775 0.815]; % default fig pos.

end

How to plot to two figures simultaneously? (4)

5 Comments

Show 3 older commentsHide 3 older comments

Hugh Wheaton on 22 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833053

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833053

Damn I was just about to get there myself! Cheers anyway. Learning the graphics of matlab's been difficult so far, I can't quite figure what objects are actually being saved etc. (Like what an axis actually is for instance, if you can make an array of handles etc.).

Adam Danz on 22 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833064

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833064

Open in MATLAB Online

"I can't quite figure what objects are actually being saved "

If you plot a line using

plot(x,y)

The line object appears on the axes and it is 'saved' there. You can access the handle by using findobj or findall or other methods. But to save the handle,

h = plot(x,y);

If x and y are matrices, there will be a handle for each column. You can also create a handle array by,

h(1) = plot(1:5, 1:5)

h(2) = plot(5:10, 5:10);

An axis is the object created by

h = axes();

% or

h = uiaxes();

% or

h = polaraxes()

% and more...

Hugh Wheaton on 23 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833316

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833316

Nah I meant more in the sense of what data structure a handle is, and when an array or structure is necessary etc.

Hugh Wheaton on 23 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833392

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833392

how to I do this to maintain the same titles? I can't copy the axis to the subplot as that is, itself, an axis, so I don't really know what to do...

Adam Danz on 23 Apr 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833636

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/519294-how-to-plot-to-two-figures-simultaneously#comment_833636

Open in MATLAB Online

To investigate a handle, you can determine what the handle is:

class(h) % h is the handle.

or just print h in the command window and the first line shows what the handle is (ie, "Line").

Then you can look up "Line properties" in the documentation which defines each property.

If you give titles to each subplot in my example, you'll see that the titles are copied to the figures. "I can't copy the axis to the subplot " -- my example copies an axes to a figure. Are you doing the opposite -- copying an axes to a subplot? If so, there are two ways to do that.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsGraphics ObjectsGraphics Object Programming

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

  • figure
  • plotting
  • subplot
  • copyobj
  • copy axes

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.


How to plot to two figures simultaneously? (10)

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

Contact your local office

How to plot to two figures simultaneously? (2024)

References

Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5447

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.