3
$\begingroup$

Suppose I plot the data and fitting line with black point and red line

imgPlotMarkers00 = 
ListLinePlot[{Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]}, 
PlotMarkers -> {Automatic, None}, PlotStyle -> {Black, Red}, 
Joined -> {False, True}]

However, I found in the output figure

enter image description here

the black points cover up the red line. How could I move the red line to the top layer of figure? Is their a option for this?

This is a simple example. In real case I have more than one data-fit line pairs to plot and their legends to placed.

$\endgroup$

5 Answers 5

4
$\begingroup$

I prefer to control what goes over what with Prolog vs Epilog (another way is to use different orders inside Show but it is more cumbersome in this case, as it requires to split 1 plot into 2):

data={#,#+RandomReal[{-1,1}/2]}&/@Range[40];

ListLinePlot[data,PlotStyle->{Red,Thickness[.01]},
   Prolog->{Black,PointSize[.02],Point[data]}]

ListLinePlot[data,PlotStyle->{Red,Thickness[.01]},
   Epilog->{Black,PointSize[.02],Point[data]}]

enter image description here

If you need PlotLegends to work for multiple data you can put lines in Epilog/Prolog and points in PlotMarkers:

data1={#,#+RandomReal[{-1,1}/2]}&/@Range[40];
data2={#,5+#+RandomReal[{-1,1}/2]}&/@Range[40];

ListPlot[{data1,data2},
    PlotLegends->{"data1","data2"},
    PlotMarkers->{Automatic,Scaled[0.05]},
    Epilog->{{Red,Thickness[.01],Line[data1]},
            {Red,Thickness[.01],Line[data2]}}]

enter image description here

$\endgroup$
5
  • $\begingroup$ emm...this is nice..but data in Prolog/Epilog would not show in PlotLegend.... $\endgroup$
    – Harry
    Commented Jun 18, 2020 at 4:28
  • $\begingroup$ @Harry You can put your line in Prolog and Epilog, and depict points with PlotMarkers. Then it will shoe up in PlotLegend doe multiple data. $\endgroup$ Commented Jun 18, 2020 at 4:32
  • $\begingroup$ That's a good idea, I can plot points with PlotMarkers, plot line both in PlotMarkers/None and Epilog, then all thing will show up in PlotLegend! $\endgroup$
    – Harry
    Commented Jun 18, 2020 at 4:55
  • $\begingroup$ @Harry I just added a version of this in the post. $\endgroup$ Commented Jun 18, 2020 at 4:59
  • $\begingroup$ @Harry and of course on top of that you can still use opacity for any of these components. $\endgroup$ Commented Jun 18, 2020 at 5:00
3
$\begingroup$

How could I move the red line to the top layer of figure?

May be change the opacity of the black points?

imgPlotMarkers00 = 
 ListLinePlot[{Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]},
  PlotMarkers -> {Automatic, None},
  PlotStyle -> {{Opacity[.3], Black}, Red},
  Joined -> {False, True}
  ]

enter image description here

$\endgroup$
3
$\begingroup$

You can post-process the ListLinePlot output to sort the graphics primitives so that lines come after markers:

ClearAll[linesInFront]
linesInFront = SubsetMap[Reverse, #, Join @@ 
   Table[Position[#, {___, {___,  i}, ___}], {i, {_Line, _GeometricTransformation}}]] &;

Example:

data = {Range[40] + RandomReal[{-1, 1}/2, 40], Range[40]};

llp = ListLinePlot[Join[data, 10 + data], 
   PlotMarkers -> {{Automatic, Medium}, None, {Automatic, Medium}, None},
   PlotStyle -> {Black, {Red, Thick}, Black, {Green, Thick}}, 
   Joined -> {False, True, False, True},
   ImageSize -> Medium, PlotLegends -> Automatic];

Row[{llp, linesInFront @ llp}, Spacer[10]]

enter image description here

Update: An alternative/simpler approach: Extract markers and use them as Prolog:

ClearAll[markersInBack]
markersInBack = Module[{markers = Cases[#, {___,_GeometricTransformation}, All]},
   Show[DeleteCases[#, Alternatives @@ markers, All], Prolog -> markers]]&;

markersInBack @ llp

enter image description here

$\endgroup$
1
  • $\begingroup$ This seems a really high level trick.... $\endgroup$
    – Harry
    Commented Jun 21, 2020 at 15:14
1
$\begingroup$

Put the red line over the black points

v={Range[60] + RandomReal[{-1, 1}/2, 60], Range[60]};
Show[
  ListLinePlot[v,PlotStyle->Black,Joined->False],
  ListLinePlot[v,PlotStyle->Red,Joined->True]
]
$\endgroup$
1
$\begingroup$
Show[ListPlot[Range[60] + RandomReal[{-1, 1}/2, 60], PlotStyle -> Black], 
 ListPlot[Range[60], PlotStyle -> Red, Joined -> True]]

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.