6
$\begingroup$

I need to plot two datasets on the same plot. The datasets have the same x-range. However, I want to show only parts of the plot.

A minimal example would be

    h = π/100.;
    i1 = ListLinePlot[Table[{i*h, Sin[i*h]}, {i, 0, 100}], PlotStyle -> Red];
    i2 = ListLinePlot[Table[{i*h, Cos[i*h]}, {i, 0, 100}], PlotStyle -> Blue];
    l1 = Graphics[{Black, Dashed, Line[{{π/2, -1}, {π/2, 1}}]}];
    Show[{i1, i2, l1}, PlotRange -> All]

The output is the following

enter image description here

But, the plot I want is

enter image description here

Can anyone please help? Thanks in advance.

$\endgroup$
2
  • $\begingroup$ Have a look at the ColorFunction option and set to White the parts you don't want to show. $\endgroup$ May 5, 2018 at 7:10
  • $\begingroup$ Thanks, @b.gatessucks. I am using 'i1 = ListLinePlot[Table[{ih, Sin[ih]}, {i, 0, 100}], PlotStyle -> Red, ColorFunction -> Function[{x, y}, If[x >= [Pi]/2, Red, White]], ColorFunctionScaling -> False];' now. It is almost there but I am getting a 'fade out effect'. Any help? $\endgroup$ May 5, 2018 at 8:14

3 Answers 3

4
$\begingroup$
h = π / 100;
{d1, d2} = Table[{i h, #[i h]}, {i, 0, 100}] & /@ {Sin, Cos};
{ms1, ms2} = {{Opacity[0], Red}, {Blue, Opacity[0]}};
{i1, i2} = ListLinePlot[#[[1]], 
   PlotStyle -> Thick, 
   Mesh -> {{π/2}}, 
   MeshStyle -> None, 
   MeshShading -> #[[2]]] & /@ 
 {{d1, ms1}, {d2, ms2}};

Show[{i1, i2}, 
   GridLines -> {{{π / 2, Directive[Thick, Dashed]}}, None}, 
   PlotRange -> All]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot, kglr. $\endgroup$ May 5, 2018 at 13:02
  • $\begingroup$ @Pratyay, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    May 5, 2018 at 13:22
4
$\begingroup$

This is a hack may be as I do not see now a direct option to do this.

Combine the 2 list plots into one, using Epilog then use Show

red   = Table[{i*h,Sin[i*h]},{i,0,100}];
blue  = Table[{i*h,Cos[i*h]},{i,0,100}];
vLine = Graphics[{Black, Dashed, Line[{{Pi/2, -1}, {Pi/2, 1}}]}]; 
p     = ListLinePlot[blue[[1;;51]],
            PlotStyle->Blue, 
            PlotRange->{{0,3},Automatic}, 
            Epilog->First@ListLinePlot[red[[51;;-1]],PlotStyle->Red]];

moment=Show[{p,vLine},PlotRange->{{0,3},{-1,1}}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ This was my first guess too. This is a simplified example. But, what I really have is a discrete data. Here the vline falls exactly on a x-data point which is not exactly my case. To clarify, I have data at x=1.2 and 1.3, and my vline is at x=1.26. $\endgroup$ May 5, 2018 at 7:55
1
$\begingroup$

Maybe

h = π/100.;
a = Table[{i*h, Sin[i*h]}, {i, 0, 100}];
b = Table[{i*h, Cos[i*h]}, {i, 0, 100}];
α = FirstPosition[a[[All, 1]] - π/2, _?Positive][[1]]
i1 = ListLinePlot[a[[α ;;]], PlotStyle -> Red];
i2 = ListLinePlot[b[[;; α]], PlotStyle -> Blue];
l1 = Graphics[{Black, Dashed, Line[{{π/2, -1}, {π/2, 1}}]}];
Show[{i1, i2, l1}, PlotRange -> All, AxesOrigin -> {0, 0}]

?

$\endgroup$

Your Answer

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

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