multiple graphs in one script (2024)

2,654 views (last 30 days)

Show older comments

summyia qamar on 28 Jul 2018

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script

Commented: Margee Pipaliya on 8 Feb 2023

Accepted Answer: Walter Roberson

I am runny a loop over set of function and in the end I want some graphs of different data. there are three variables let say a, b and c.I have generated a graph using:

plot(iteration,a,iteration,b,iteration,c)

plot(a,b)

bt it is giving the 2nd one only. I want another graph of A on x axis and C on y axis likewise C on x axis and b on y axis. I cant figure out how to get multiple plots because it is generating plot of last command only. I tried

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Walter Roberson on 28 Jul 2018

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#answer_330733

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#answer_330733

Do not use figure() as a variable name.

fig1 = figure(1);

ax1 = axes('Parent', fig1);

h = plot(ax1, Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');

legend(h, 'total_util','d_quantity','Price');

fig2 = figure(2);

ax2 = axes('Parent', fig2);

h = plot(ax2, total_util);

The simple version of that is

figure(1);

plot(Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');

legend('total_util','d_quantity','Price');

figure(2);

plot(total_util);

but I recommend using the longer version: with the shorter version, graphics can end up going places you do not want in some cases. See https://www.mathworks.com/matlabcentral/answers/?term=tag%3Aalways-parent

1 Comment

Show -1 older commentsHide -1 older comments

Margee Pipaliya on 8 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_2605140

Really helpful

Sign in to comment.

More Answers (1)

Ben Frankel on 28 Jul 2018

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#answer_330721

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#answer_330721

Edited: Ben Frankel on 28 Jul 2018

You need to use hold on after the first plot and hold off after the last plot if you want multiple plots in the same figure. Use figure before all of the plots to put them in a new figure.

10 Comments

Show 8 older commentsHide 8 older comments

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594204

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594204

this is not working because both plots are showing on the same graph I want 2 different graph windows

Walter Roberson on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594205

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594205

  • "hold on" if you want the graphs on the same axes
  • subplot() if you want the graphs on different axes in the same figure
  • figure() if you want to create different windows

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594208

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594208

with figure(1) it is giving indexing error

Walter Roberson on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594210

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594210

If figure(1) is giving an indexing error, then you somehow have accidentally created an empty variable named figure

which figure

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594211

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594211

I have written this after the loop end.. and no other FIGURE word used

figure(1)=plot(Iteration,total_util*100,'b',Iteration,DD,'r',Iteration,PP,'g');

legend('total_util','d_quantity','Price')

figure(2)=plot(total_util);

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594213

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594213

Error

Unable to perform assignment

because the left and right sides

have a different number of

elements.

figure(1)=plot(Iteration,total_util*100,'b',Iteration,DD,'r',Iteration,PP,'g');

Walter Roberson on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594214

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594214

Do not use figure() as a variable name.

fig1 = figure(1);

ax1 = axes('Parent', fig1);

h = plot(ax1, Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');

legend(h, 'total_util','d_quantity','Price');

fig2 = figure(2);

ax2 = axes('Parent', fig2);

h = plot(ax2, total_util);

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594215

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594215

yes it worked. thankyou

summyia qamar on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594216

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594216

isn't there any easy way? a one line function?

Walter Roberson on 28 Jul 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594224

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/412488-multiple-graphs-in-one-script#comment_594224

figure(1);

plot(Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');

legend('total_util','d_quantity','Price');

figure(2);

plot(total_util);

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D Plots

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

  • plot
  • multiple graphs

Products

  • MATLAB

Release

R2018a

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.


multiple graphs in one script (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

multiple graphs in one script (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5971

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.