{"id":2200,"date":"2010-01-20T18:56:37","date_gmt":"2010-01-20T17:56:37","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2200"},"modified":"2010-01-20T18:56:37","modified_gmt":"2010-01-20T17:56:37","slug":"plotting-in-a-loop-in-mathematica","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2200","title":{"rendered":"Plotting in a loop in Mathematica"},"content":{"rendered":"<p>Ever since version 6 of Mathematica was released (way back in 2007) I have received dozens of queries from users at my University saying something along the lines of &#8216;Plotting no longer works in Mathematica&#8217; or &#8216;If I wrap my plot in a Do loop then I get no output.\u00a0 The user usually goes on to say that it worked perfectly well in pre-v6 versions of Mathematica, is clearly a bug and could I please file a bug report with Wolfram Research?<\/p>\n<p>It&#8217;s not a bug&#8230;.it&#8217;s a feature!<\/p>\n<p>If you have googled your way here and just want a fast solution to your problem then here goes.\u00a0 I assume that you have code that looks like this<\/p>\n<pre>Do[\r\n Plot[Cos[n x], {x, -Pi, Pi}]\r\n , {n, 1, 3}\r\n ]<\/pre>\n<p>and you were expecting to get a set of nice plots (in this case 3) &#8211; one plot for each iteration of your loop.  Instead, Mathematica 6 or above is giving you nothing&#8230;nada&#8230;zip!  There are several ways you can &#8216;fix&#8217; this and I am going to show you two.  Option 1 is to wrap your Plot Command with Print<\/p>\n<pre>Do[\r\n Print[Plot[Cos[n x], {x, -Pi, Pi}]]\r\n , {n, 1, 3}\r\n ]<\/pre>\n<p>Option 2 is to use Table instead of Do<\/p>\n<pre>Table[\r\n Plot[Cos[n x], {x, -Pi, Pi}]\r\n , {n, 1, 3}\r\n ]<\/pre>\n<p>I am imaging that the average googler has now disappeared having got what they came for but some of you are possibly wondering why the above two &#8216;fixes&#8217; work.\u00a0 Here&#8217;s my attempt at an explanation.<\/p>\n<p>Old versions of Mathematica (v5.2 and below) treated graphics very differently to the way that Mathematica works today.  The output of a command such as Plot[] used to contain two parts &#8211; the first was a Graphics object which looked a bit like the following in the notebook front end<\/p>\n<p><strong>&#8211; Graphics &#8211;<\/strong><\/p>\n<p>This was the OutputForm of the actual symbolic result of the Plot[] command.  The second part &#8211; the actual visualization of the plot &#8211; was effectively just a side effect that happened to be what us humans actually wanted.<\/p>\n<p>In version 6 and above, the OutputForm of a Graphics object is its visualisation which makes a lot more sense then just <strong>&#8211; Graphics &#8211;<\/strong>.  The practical upshot of this is that there is now no need for a &#8216;side effect&#8217; since the visualization of the plot IS the output.\u00a0 If you are confused then this bit is explained much more eloquently in an <a href=\"http:\/\/blog.wolfram.com\/2007\/06\/08\/in-mathematica-pictures-are-worth-a-thousand-words\/\">old Wolfram Blog post<\/a>.<\/p>\n<p>That&#8217;s the first part of the story regarding plots in loops.\u00a0 The second piece of the puzzle is to know that when you wrap a Mathematica expression in a Do loop then <strong>you suppress its output but you don&#8217;t suppress any side effects<\/strong>.\u00a0 In version 5.2 the plot is a side effect and so it gets displayed on each iteration of the loop.\u00a0 In version 6 and above, the plot is the output and so gets suppressed unless you explicitly ask for it to be displayed using Print[].<\/p>\n<p>Does this help clear things up?\u00a0 Comments welcomed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever since version 6 of Mathematica was released (way back in 2007) I have received dozens of queries from users at my University saying something along the lines of &#8216;Plotting no longer works in Mathematica&#8217; or &#8216;If I wrap my plot in a Do loop then I get no output.\u00a0 The user usually goes on [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8,7],"tags":[],"class_list":["post-2200","post","type-post","status-publish","format-standard","hentry","category-mathematica","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-zu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2200"}],"version-history":[{"count":8,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions"}],"predecessor-version":[{"id":2208,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions\/2208"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}