Re: how to plot new data points to pre-existing figure
- To: mathgroup at smc.vnet.net
- Subject: [mg122885] Re: how to plot new data points to pre-existing figure
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Mon, 14 Nov 2011 07:07:39 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j9o3j9$15$1@smc.vnet.net>
On Sun, 13 Nov 2011 09:45:13 -0000, tle <truongvle at yahoo.com> wrote: > Hi, > > Could someone please tell me how to delete the old figure and create a > new one in a for loop. > > thanks, > > testPlot[k_] := Module[{i, n, m, z}, > z[x_, m_] = m*x; > For[i = 1, i < k, > Print[{i}]; > plot1 = > Plot[z[x, i], {x, 0, 10}, PlotRange -> {{0, 10}, {0, 20}}]; > Print[plot1]; > i++]; > ] > Your question is clearly more concerned with the mechanics of this operation rather than the end result, so the following mimics as closely as possible what you have asked for--although the reply already given by Bob Hanlon details what would usually be considered a more sensible approach. However, since the subject of your posting is inconsistent with the actual message (do you want to plot new points on an existing figure, or to delete the figure and replace it with a new one?), I have chosen to interpret the question as per the subject line. This is mainly because in order to explicitly delete an output and replace it with something else one needs to be familiar with notebook programming, which will most likely only confuse the issue. Your procedure as given above has several bugs: unused Module variables; z defined using Set rather than SetDelayed, so x is not bound to the definition; and an off-by-one error in the For loop. I have corrected these as well. The definition of testPlot can then be restated as follows: testPlot[k_] := Block[{i, z, pr}, z[x_, m_] := m*x; pr = PlotRange -> {{0, 10}, {0, 20}}; DynamicModule[{prims = {}, label = Null}, (* The figure to be updated *) Print@Graphics[ Dynamic[prims], PlotLabel -> Dynamic[label], pr, Axes -> True, AspectRatio -> 1/GoldenRatio ]; For[i = 0, i < k, i++; (* Label the figure with the iteration number and then use Plot to generate new graphics primitives *) label = i; prims = First@Plot[z[x, i], {x, 0, 10}, Evaluate[pr]]; Pause[1/2] ] ]; ]; Here I have inserted a half-second pause between each iteration of the loop. This is not required, but without it the figure is updated too rapidly to see what is happening. Best, O. R.