9
$\begingroup$

I have an external program which generates a set of data - X points at the time t1, X points at the time t2 and so on. These points are stored in CSV file and I can change the format if necessary.
What is the easiest way to plot and manipulate them, so I can click "play" and see the dynamics of process?

UPD:
For example, points at t1:

0.2 42.8
0.4 12.3
0.6 32.1
0.8 37.3

Points at t2:

0.2 44.2
0.4 17.8
0.6 39.0
0.8 30.1

It is always a constant number of points at any time (4 in example). I can export them to separate CSV files or in the one file and change format in any way necessary, like adding a new column.

$\endgroup$
3
  • $\begingroup$ Welcome, Andrew. Please consider posting a small sample of your data, or a representative approximation. Also, what kind of plot do you desire? $\endgroup$
    – Mr.Wizard
    Jun 9, 2012 at 9:00
  • $\begingroup$ @Mr.Wizard Thanks, I have updated the question. Line plot. $\endgroup$
    – Andrew
    Jun 9, 2012 at 9:13
  • $\begingroup$ You could, you know, do something like Map[ListPlot, {t1Points, t2Points, ...}] and then use ListAnimate[] on those... $\endgroup$ Jun 9, 2012 at 9:17

2 Answers 2

10
$\begingroup$

Load your data with Import["data.csv"] and process as necessary into:

data = {
  {{0.2`, 42.8`}, {0.4`, 12.3`}, {0.6`, 32.1`}, {0.8`, 37.3`}},
  {{0.2`, 44.2`}, {0.4`, 17.8`}, {0.6`, 39.`},  {0.8`, 30.1`}}
  };

Build a sequence of plots with a fixed PlotRange to stabilize the image, and ListAnimate:

ListAnimate[
   ListLinePlot[#, PlotRange -> {10, 50}] & /@ data
]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Thanks, this is exactly what I needed. $\endgroup$
    – Andrew
    Jun 9, 2012 at 9:23
  • 2
    $\begingroup$ @Andrew you're welcome. Please make your future questions as specific as possible, and include sample data and executable code when appropriate. $\endgroup$
    – Mr.Wizard
    Jun 9, 2012 at 9:26
21
$\begingroup$

Then add some salt

q = 20;
pos = Table[.1 i, {i, q}];
{start, end} = RandomReal[1, {2, q}];
pts[i_Integer, t_Real] := {pos[[i]], (1 - t) start[[i]] + t end[[i]]}
c[t_Real] := Interpolation[Table[pts[i, t], {i, q}], Method -> "Spline"];

Manipulate[
  Plot[c[t][x], {x, 0.1, 2},
   PlotRange -> {{0, 2.1}, {-.5, 1.2}},
   Epilog -> {
     Red,   PointSize[0.01], Point@Table[pts[i, t], {i, q}],
     Green, Line@Partition[Riffle[Transpose[{pos, start}], Transpose[{pos, end}]], 2]
     }],
  {t, 0.01, .99, .05}]

enter image description here

Edit

Found some code I used in a project eons ago. You could do very weird things with your points' trajectories if you take some precautions:

(*Let's create a points file sample *)
s = Transpose@Table[{i + Cos[2 Pi k/10], Sin[2 Pi i/10] + Sin[2 Pi (k + 3)/10]}, 
                   {i, 0, 10, 1.}, {k, 0, 10, 1.}];
Export["c:\\table.csv", Flatten[s, 1]];
ClearAll[s];

    (*Main fun def*)
maniPointPlot[pts_, n_] := Module[{sets, steps, curve, pp, enlarge, bounds, apt},

   sets = MapIndexed[{#2[[1]], #1} &, Partition[pts, n], {2}];(*Add time indicator*)
   bounds = {Min@#, Max@#} & /@ Transpose@pts;(*Bounds for Plot*)
   steps = Length@sets;(*Time span*)

   (*Memoize Positions of point # over time " / All Points Together*)
   apt[t_] := apt[t] = Interpolation[#, Method -> "Spline"][t] & /@ Transpose@sets;
   (*Curve at time t*)
   curve[t_] := Interpolation[apt@t, Method -> "Spline"];

   (*aux func for interval dilation*)
   enlarge[{a_, b_}] := {a - #, b + #} &@(Abs[b - a]/10);
   (*Green Points traces*)
   pp = ParametricPlot[apt@t, {t, 1, steps}, ColorFunction -> (Green &)];

   (*Our little beast follows*)
   Manipulate[
    Show@{Plot[curve[t][x], Evaluate@{x, Sequence@@bounds[[1]]}, PlotRange -> enlarge /@ bounds],
          Graphics[Point@apt@t],
          pp}, {t, 1, steps}]];

(*Now your code*)

s = Import["c:\\table.csv"]; (*Read the table*)
nbr = 11; (*number of points in each set*)
Off[InterpolatingFunction::dmval];
maniPointPlot[s, nbr]

enter image description here

Cozy crawling cardioids:

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ And don't forget the bacon... because everything is better with bacon. $\endgroup$
    – jmlopez
    Jun 9, 2012 at 22:18
  • $\begingroup$ @jmlopez I was going to said something about the beer. Byt i"n two drjnk tp rimemver wat uas itjs/ $\endgroup$ Jun 9, 2012 at 22:29
  • 1
    $\begingroup$ Que? lol, I should go grab one too... $\endgroup$
    – jmlopez
    Jun 9, 2012 at 22:31
  • 3
    $\begingroup$ Okay that's pretty slick. +1 $\endgroup$
    – Mr.Wizard
    Jun 11, 2012 at 1:50
  • $\begingroup$ Are you a wizard? $\endgroup$
    – Andrew
    Jun 11, 2012 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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