3
$\begingroup$

I am trying to add new row or column or points to a 2D plot as I keep generating the values. The trick is by using this method I will not be required to store the big array or matrix and will be helpful in plotting suppose $50000\times 4000$ array size. By the end of the loop, I want to the get 2D plot without storing array or matrix. See the example code.

This could be implemented in MATLAB as initializing figure with some size and properties, hold on and adding each row or data point as generated.

a = Table[{0}, {5000}, {6000}]; (* initialize*)

For[k = 1, k <= 5000, k++,

line = 2*k*Range[1,6000]; (* random way of getting data value at (k,t) index*)

(* here write graphics command to add new row points at index (k,t) to the 2D plot*)

]
$\endgroup$
  • $\begingroup$ Did you succeed in plotting your 50000x4000 array with Matlab ? If so you have enough memory to do the same with Mathematica. The Show approach can deal with that, though the solution proposed below do not show how to use it with arrays. I might have a solution for you. $\endgroup$ – SquareOne Nov 14 '15 at 16:11
  • $\begingroup$ @SquareOne I am able to Animate however as you mentioned, not enough memory to plot large array. I am still looking for the solution. $\endgroup$ – Veena Nov 15 '15 at 17:01
  • $\begingroup$ I don't understand: 1/ what do you exactly Animate ? 2/you did that (Animate) with Matlab or Mathematica ? $\endgroup$ – SquareOne Nov 15 '15 at 17:22
3
$\begingroup$

A possible solution is to not store the data, but rather to store plots of the data at each step. These plots can be combined together using Show, but the data that were used to generate them can be discarded.

xmin = 1;
xmax = 5;
xstep = 1;
plotList = {};

Monitor[
  Do[
    AppendTo[plotList, 
     Plot[x Cos[x], 
         {x, 0 + (i*2 Pi), 2 Pi + (i*2 Pi)}, 
         PlotStyle -> ColorData[i, "ColorList"]]];
    Pause[2];
   , {i, xmin, xmax, xstep}];
 , Show[plotList, PlotRange -> All]
 ]

animation

The final plot (shown here) demonstrates that the different segments were plotted with different colors:

Show[plotList, PlotRange -> All]

segments

$\endgroup$
  • 1
    $\begingroup$ I'd probably be using Table[] here. $\endgroup$ – J. M. is away Nov 7 '15 at 22:14
  • $\begingroup$ Table for what? Data generation? e.g. ListPlot? $\endgroup$ – Eric Brown Nov 7 '15 at 22:31
  • $\begingroup$ Thank you. I got some idea. I am basically looking for output similar to Matrix or Array plot, however by adding each column to the 2D figure as I don't have matrix available at one time. May be ListDensity or scatter functions can be used with this solution? $\endgroup$ – Veena Nov 7 '15 at 22:45
  • 1
    $\begingroup$ I mean, instead of a Do[] loop with AppendTo[], I'd use Table[] instead: plotList = Table[Plot[x Cos[x], {x, 0 + (i*2 Pi), 2 Pi + (i*2 Pi)}, PlotStyle -> ColorData[i, "ColorList"]]], {i, xmin, xmax, xstep}]; Then, generate the animation with Animate[Show[Take[plotList, k], (* options *)], {k, 1, Length[plotList]}]. But if the purpose was to be able to use Monitor[], then yes, this is okay, but I'd still cast a jaundiced eye over AppendTo[]. $\endgroup$ – J. M. is away Nov 7 '15 at 22:47
  • $\begingroup$ @J.M. Yes, this is preferable if all the data are available at t=0, and the number of additions to the plot are known. I do this sort of thing that I have shown in e.g. monitoring MCMC evolution. $\endgroup$ – Eric Brown Nov 7 '15 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.