3
$\begingroup$

I have a list (imported from csv file) of the form

data = {{1, "26/06/2006 15:00", 3, 0}, {2, "26/06/2006 15:05", 8, 0}, 
        {3, "26/06/2006 15:10", 9, 0}, {4, "26/06/2006 15:15", 8.5, 0},
        {5, "26/06/2006 15:20", 7, 0}, {6, "26/06/2006 15:25", 6, 1},
        {7, "26/06/2006 15:30", 3, 0}}

I want to plot the third and forth columns, where the third, the sample value, is joined, the forth, a flag, is not.

I only want to have a marker for the second option if it's value is 1 (not zero more generally). I have tried a couple of options that don't quite work.

ListPlot[{data[[;;, 3]], data[[;;, 4]]*10}, 
  Joined -> {True, False}, Filling -> Axis]

Ideally I don't want the first set with Filling, but I couldn't get it to use different arguments for both with

Filling -> {None, Axis}

I also don't want the 0 markers to show.

Effectively my 0/1 are just flags, I'll manually set a multiple for this to show nicely on the plot.

I tried to use Select, however this created a new list of just the 1 flags.

My actual dataset is many hundreds of thousands of lines, this is just a small example.

I have also tried making two separate plots, restricting the PlotRange and using Show, but as I need 0 in the first plot this didn't work. I could solve the filling problem this way.

EDIT: Error in the sample data, the first column should have indicated the x-value/ID

$\endgroup$
4
$\begingroup$

You can use Epilog for that. Say, you have a data and you want to put markers only if the third element is 3 or 4.

data = {#, RandomReal[], RandomInteger[5]} & /@ Range[50];

c3 = Blue; s3 = Select[data, #[[3]] == 3 &][[All, {1, 2}]];
c4 = Red;  s4 = Select[data, #[[3]] == 4 &][[All, {1, 2}]];

ListLinePlot[data[[All, {1, 2}]], Epilog -> {PointSize[Large],
                                 c3, Point[s3], c4, Point[s4]}]

enter image description here

$\endgroup$
2
$\begingroup$

Maybe this will work for you.

data = 
   ({1, "26/06/2006 15:00", 3, 0},
    {1, "26/06/2006 15:05", 8, 0},
    {1, "26/06/2006 15:10", 9, 1},
    {1, "26/06/2006 15:15", 8.5, 0},
    {1, "26/06/2006 15:20", 7, 0},
    {1, "26/06/2006 15:25", 6, 1},
    {1, "26/06/2006 15:30", 3, 0}};

plotData = 
  With[{k = 10, pattern = {_, _, u_, v_}}, 
    Transpose[data /. {pattern /; v > 0 :> {u, 10 v}, pattern :> {u, Null}}]]

{{3, 8, 9, 8.5, 7, 6, 3}, {Null, Null, 10, Null, Null, 10, Null}}

ListPlot[plotData,
  Joined -> {True, False},
  Filling -> 2 -> Axis,
  PlotStyle -> {Blue, Red}]

plot

Update

This may be faster when there a lot of data points.

With[{k = 10},
  helper[{_, _, u_, v_ /; v > 0}] := {u, 10 v};
  helper[{_, _, u_, v_}] := {u, Null};
  plotData = Transpose[helper /@ data]]

Also you might consider automatically placing the markers on the plot.

helper[{_, _, u_, v_ /; v > 0}] := {u, u}
helper[{_, _, u_, v_}] := {u, Null}
plotData = Transpose[helper /@ data];

ListPlot[plotData,
  Joined -> {True, False},
  Filling -> 2 -> Axis,
  PlotStyle -> {Blue, Red}]

alt_plot

$\endgroup$
1
$\begingroup$

I set one more flag just to make the data more interesting.

data = {{1, "26/06/2006 15:00", 3, 1}, {1, "26/06/2006 15:05", 8, 0}, 
        {1, "26/06/2006 15:10", 9, 0}, {1, "26/06/2006 15:15", 8.5, 0}, 
        {1, "26/06/2006 15:20", 7, 0}, {1, "26/06/2006 15:25", 6, 1}, 
        {1, "26/06/2006 15:30", 3, 0}};
vals = data[[All, 3 ;; 4]] /. {___, _?(# == 0. &), ___} :> Sequence[];
ListPlot[vals[[All, 1]], Joined -> True, Filling -> Axis]

enter image description here

$\endgroup$
0
$\begingroup$

Really just because I leak Reap and Sow:

data = {{1, "26/06/2006 15:00", 3, 0}, {1, "26/06/2006 15:05", 8, 
    0}, {1, "26/06/2006 15:10", 9, 1}, {1, "26/06/2006 15:15", 8.5, 
    0}, {1, "26/06/2006 15:20", 7, 0}, {1, "26/06/2006 15:25", 6, 
    1}, {1, "26/06/2006 15:30", 3, 0}};
{lp, p} = 
  Reap[MapIndexed[Sow[{#2[[1]], #1[[1]]}, #1[[2]]] &, 
    data[[All, {3, 4}]]], 1, #2 &];
ListPlot[lp, Epilog -> {Red, PointSize[0.02], Point[p[[1]]]}, 
 Joined -> True]

enter image description here

or using Sumit's data:

test = data = {#, RandomReal[], RandomInteger[5]} & /@ Range[50];
{tlp, tp} = Reap[Sow[{#1, #2}, #3] & @@@ test, 3 | 4, #2 &];
ListPlot[tlp, Joined -> True, 
 Epilog -> 
  MapThread[{#2, PointSize[0.02], Point[#1]} &, {tp, {Red, Blue}}]]

enter image description here

$\endgroup$

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.