Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6421 > unrolled thread

how to plot new data points to pre-existing figure

Started bytle <truongvle@yahoo.com>
First post2011-11-13 09:45 +0000
Last post2011-11-14 12:11 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  how to plot new data points to pre-existing figure tle <truongvle@yahoo.com> - 2011-11-13 09:45 +0000
    Re: how to plot new data points to pre-existing figure Peter Pein <petsie@dordos.net> - 2011-11-14 12:06 +0000
    Re: how to plot new data points to pre-existing figure "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-14 12:11 +0000

#6421 — how to plot new data points to pre-existing figure

Fromtle <truongvle@yahoo.com>
Date2011-11-13 09:45 +0000
Subjecthow to plot new data points to pre-existing figure
Message-ID<j9o3j9$15$1@smc.vnet.net>
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++];
  ]

[toc] | [next] | [standalone]


#6429

FromPeter Pein <petsie@dordos.net>
Date2011-11-14 12:06 +0000
Message-ID<j9r08m$bc3$1@smc.vnet.net>
In reply to#6421
Am 13.11.2011 10:45, schrieb tle:
> 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++];
>    ]
>

Try PrintTemporary:

testPlot[k_] :=
  Module[{i, n, m, z, tmp = PrintTemporary[""], plot1},
   z[x_, m_] = m*x;
   For[i = 1, i < k,
    plot1 =
     Plot[z[x, i], {x, 0, 10}, PlotRange -> {{0, 10}, {0, 20}},
      PlotLabel -> Style["i == " <> ToString[i], 12]];
    NotebookDelete[tmp];
    tmp = PrintTemporary[plot1];
    Pause[1];
    i++];
   plot1
   ]

testPlot[3]

[toc] | [prev] | [next] | [standalone]


#6435

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-11-14 12:11 +0000
Message-ID<j9r0ha$bhk$1@smc.vnet.net>
In reply to#6421
On Sun, 13 Nov 2011 09:45:13 -0000, tle <truongvle@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web