1
$\begingroup$

This is a small sample of my data

data = {4., 13.7107, 8.70956, 2.39369, -1.952, 2.923}, {4.1, 
13.7263, 8.7468, 2.39592, -1.818, 2.712}, {4.2, 13.7425, 8.78553, 
2.39829, -1.591, 2.49}, {4.3, 13.7591, 8.82563, 2.40079, -1.257, 
2.251}, {4.4, 13.7762, 8.86697, 2.40342, -0.811, 1.988}, {4.5, 
13.7937, 8.90944, 2.40617, -0.244, 1.693}, {4.6, 13.8117, 8.95293, 
2.40902, 0.448, 1.345}, {4.7, 13.8302, 8.99732, 2.41199, 1.274, 
0.88}, {4.8, 13.849, 9.04256, 2.41505, 2.231, 1.61}, {4.9, 13.8682, 
9.08847, 2.4182, 3.33, 2.996}, {5., 13.8879, 9.13509, 2.42145, 
4.564, 4.333}, {5.1, 13.9079, 9.18224, 2.42478, 5.933, 5.76}, {5.2, 
13.9282, 9.22989, 2.42819, 7.434, 7.297}, {5.3, 13.9489, 9.27795, 
2.43168, 9.061, 8.949}, {5.4, 13.97, 9.32636, 2.43525, 10.816, 
10.723}, {5.5, 13.9914, 9.37507, 2.43889, 12.686, 12.607}, {5.6, 
14.0131, 9.42401, 2.4426, 14.661, 14.593}, {5.7, 14.0351, 9.47312, 
2.44638, 16.72, 16.66}, {5.8, 14.0575, 9.52236, 2.45022, 18.876, 
18.823}, {5.9, 14.0802, 9.57168, 2.45413, 21.077, 21.029}, {6., 
14.1031, 9.62103, 2.45811, 23.335, 23.292}}

Then I split them into two sublists

datas = {};
datau = {};
Do[
 s = data[[i, 5]];
 If[s >= -2 && s <= 2, AppendTo[datas, {data[[i, 1]], data[[i, 2]]}], 
 AppendTo[datau, {data[[i, 1]], data[[i, 2]]}]], {i, 1, Length[data]}
]

And finally I plot them

L0 = ListPlot[{datas, datau}, 
PlotStyle -> {{Darker[Green], Thick}, {Red, Thick}}, Joined -> True,
Axes -> False, Frame -> True, 
FrameLabel -> {"a", Subscript["x", "0"]}, RotateLabel -> False, 
FrameStyle -> Directive[FontSize -> 18, FontFamily -> "Helvetica"], 
PlotRange -> {{0, 10}, {13.4, 15.5}}, ImageSize -> 550]

enter image description here

We see that about a = 4.8 there is a small gap between the green and the red line. Any suggestion on how to eliminate this gap?

Many thanks in advance!

$\endgroup$

2 Answers 2

2
$\begingroup$

First, your code does not produces same plot.

Second, you can get the data you want like this:

datas = Cases[data, {x_, y_, _, _, z_ /; -2 <= z <= 2, __} :> {x, y}];

datau = Cases[data, {x_, y_, _, _, z_ /; Not[-2 <= z <= 2], __} :> {x, y}];

Third, you need to add the end point of datas to datau

datau1 = Prepend[datau, datas[[-1]]];

or the opposite

datas1 = Append[datas, datau[[1]]];

and then plot using {datas1,datau} or using {datas,datau1}

$\endgroup$
1
  • $\begingroup$ Thanks! It works fine. I provided here a small sample of my data and that's why my code (not cold!!!) does not produce the same plot. $\endgroup$
    – Vaggelis_Z
    Jan 22, 2015 at 9:37
0
$\begingroup$

You can get datas and datau from data using SplitBy:

{datas, datau} = #[[All, {1, 2}]] & /@ SplitBy[data, -2 <= #[[5]] <= 2 &]

You can use Mesh and MeshShading to plot a single line with two colors with no gap:

ListLinePlot[data[[All, {1, 2}]], 
 Mesh -> {{(datas[[-1, 1]] + datau[[1, 1]])/2}}, MeshStyle -> None, 
 MeshShading -> {Directive[Thick, Green], Directive[Thick, Red]}]

enter image description here

$\endgroup$

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.