3
$\begingroup$

I have an external data file containing the elements (t,x,y,px,py) of an orbit. Here is a sample data file.

Now I want to ListPlot the (x,y) coordinates of the orbit and each (x,y) point to have a color according to the corresponding value of time t (first row).

data2 = data[[All, {2, 3}]];
L0 = ListPlot[data2, Frame -> True, Axes -> False, Joined -> True, 
      AspectRatio -> 1]

enter image description here

At the right side of the plot it should be a colorbar explaining the color code (rainbow colors, red ---> small t, purple ---> high t). The colorbar should run from the minimum value of time tmin up to maximum tmax where

data3 = Table[data[[i, 1]], {i, 1, Length[data]}];
tmin = Min[data3];
tmax = Max[data3];
Print["t_min = ", tmin]
Print["t_max = ", tmax]

Any suggestions?

$\endgroup$
  • 1
    $\begingroup$ As a start: Graphics[Line[data[[All, {2, 3}]], VertexColors -> (ColorData["ThermometerColors"] /@ Rescale[data[[All, 1]]])]]. $\endgroup$ – J. M. will be back soon Nov 10 '15 at 11:14
  • $\begingroup$ @J.M. Very good but there are some issues. (a) How to control the thickness of the line (b) how to create the colorbar? (c) use Rainbow colors with red for low values of t and purple for high values of t. Perhaps an answer incorporating these issues should be more illuminating. $\endgroup$ – Vaggelis_Z Nov 10 '15 at 11:17
  • $\begingroup$ @J.M. Minus one issue. I found how to control the thickness of the line. $\endgroup$ – Vaggelis_Z Nov 10 '15 at 11:23
  • 1
    $\begingroup$ Not really a fan of the rainbow for scientific visualization, TBH. As for the colorbar, look up BarLegend[]. $\endgroup$ – J. M. will be back soon Nov 10 '15 at 11:34
3
$\begingroup$

I find the way myself!

valrange = {tmin, tmax};
data[[All, 1]] = Rescale[data[[All, 1]] // N, valrange];
colfunc[x_, cf_] := ColorData[cf][1 - x[[1]]];

S0 = Graphics[{Thickness[0.005], Line[data[[All, {2, 3}]], 
VertexColors -> (ColorData["Rainbow"][1 - #] & /@ 
   Rescale[data[[All, 1]]])]}];

Now we have to build a custom colorbar

Clear[colorbar]
colorbar[{min_, max_}, colorFunction_: Automatic, divs_: 150] := 
DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10, 
PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0, 
Frame -> True, FrameLabel -> {{None, "t"}, {None, None}}, 
RotateLabel -> False, 
LabelStyle -> Directive[FontFamily -> "Helvetica", 20], 
FrameTicks -> {{None, All}, {None, None}}, 
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain], 
ColorFunction -> colorFunction]

and finally combine the Graphics with the custom colorbar

plot = With[{opts = {ImageSize -> {Automatic, 500}}, cf = "Rainbow"}, 
       Row[{Show[{S0}, Axes -> False, Frame -> True, 
       FrameLabel -> {"x", "y"}, RotateLabel -> False, 
       LabelStyle -> Directive[FontFamily -> "Helvetica", 20], 
       AspectRatio -> 1, PlotRange -> 55, PlotRangeClipping -> True, 
       PlotRangePadding -> 0, AspectRatio -> 1, 
       ImagePadding -> {{80, 10}, {60, 40}}, opts], 
       Show[colorbar[valrange, ColorData[cf][1 - #] &], 
       ImagePadding -> {{10, 60}, {60, 40}}, opts]}]]

enter image description here

$\endgroup$
  • 3
    $\begingroup$ Well done, you! A tiny tip: ColorData["Rainbow"][1 - #] & is better done as ColorData[{"Rainbow", "Reverse"}]. $\endgroup$ – J. M. will be back soon Nov 10 '15 at 13:48
  • $\begingroup$ @J.M. Thanks for the tip! $\endgroup$ – Vaggelis_Z Nov 10 '15 at 13:50
  • 1
    $\begingroup$ Here's your colorbar slightly improved: colorbar[{min_, max_}, colorFunction_: Automatic, divs_: 150] := DensityPlot[y, {x, 0, (max - min)/10}, {y, min, max}, AspectRatio -> Automatic, BaseStyle -> Directive[FontFamily -> "Helvetica", 20, Plain], ColorFunction -> colorFunction, FrameLabel -> {{None, Rotate["t", -90 °]}, {None, None}}, FrameTicks -> {{None, All}, {None, None}}, MaxRecursion -> 0, PlotPoints -> {2, divs}, PlotRangePadding -> None] I take it you're not on the newest version to be able to use BarLegend[]? $\endgroup$ – J. M. will be back soon Nov 10 '15 at 14:04
  • $\begingroup$ @J.M. Why (max - min)/10 is better? This is the only difference I observe. I have v10.2 of MMA in my Win 7 PC. What would be the equivalent of BarLegent? $\endgroup$ – Vaggelis_Z Nov 10 '15 at 14:13
  • $\begingroup$ Only because I set AspectRatio -> Automatic. 10.2 should have BarLegend[]; why not use that instead of colorbar[]? $\endgroup$ – J. M. will be back soon Nov 10 '15 at 14:27
5
$\begingroup$
opts1 = Hue /@ (data[[All, 1]]/10) (*Color*)
opts2 = Thickness[(1 + #)/700] & /@ data[[All, 1]] (*Thickness*)

Row[{Graphics[Flatten[Riffle[Line /@ Partition[data[[All, {2, 3}]], 2, 1],
Transpose[{opts1, opts2}]]], Frame -> True, ImageSize -> 300], 
BarLegend[{Hue, {Min[data[[All, 1]]], Max[data[[All, 1]]]}}]}]

enter image description here

$\endgroup$
  • $\begingroup$ What if I want Rainbow colors instead of Hue? Also is it possible the colorbar to have the same height as the square of the plot? $\endgroup$ – Vaggelis_Z Nov 10 '15 at 12:29
  • $\begingroup$ Replace Hue /@ (data[[All, 1]]/10) by ColorData["Rainbow"] /@ (data[[All, 1]]/10) and in the legend Hue by ColorData["Rainbow"] $\endgroup$ – Coolwater Nov 10 '15 at 12:34

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.