0
$\begingroup$

I want to put the markers symbol and the line type together in the Plotlegend. I have used the following code

Data = {{{0, 1}, {2, 2}, {4, 3}, {6, 4}, {8, 4}, {10, 3}, {12, 
     6}, {14, 2}, {16, 3}, {18, 4}, {20, 8}},
   {{0, 5}, {2, 4}, {4, 3}, {6, 8}, {8, 2}, {10, 6}, {12, 1}, {14, 
     3}, {16, 5}, {18, 1}, {20, 2}},
   {{0, 9}, {2, 8}, {4, 4}, {6, 8}, {8, 5}, {10, 7}, {12, 1}, {14, 
     0}, {16, 0}, {18, 5}, {20, 4}},
   {{0, 3}, {2, 8}, {4, 2}, {6, 1}, {8, 2}, {10, 9}, {12, 0}, {14, 
     8}, {16, 5}, {18, 4}, {20, 7}}};
Xgrid = Range[0, 20, 1];
Ygrid = Range[0, 150, 5];
Ygrid1 = Range[0, 300, 10];
lineColors = {Blue, Red, Blue, Red};
lineWidths = {Thickness[0.003], Dashing, Thickness[0.003], 
   Thickness[0.003]};
markers1 = {Graphics[Rectangle[], ImageSize -> 12], 
   Graphics[Rectangle[], ImageSize -> 12], 
   Graphics[Disk[], ImageSize -> 14], 
   Graphics[Disk[], ImageSize -> 14]};
ListPlot[Data, Frame -> True, Joined -> True, PlotMarkers -> markers1,
  PlotRange -> All, ImageSize -> 800,
 PlotLabel -> Style["m", Black, 32],
 FrameStyle -> Directive[Black, 28, Thick],
 PlotStyle -> {Directive[Red, Thick], Directive[Red, Thick, Dashed], 
   Directive[Blue, Thick], Directive[Blue, Thick, Dashed]},
 FrameLabel -> {"a", "b"},
 PlotLegends -> Placed[SwatchLegend[{"1", " 2", " 3", "4"},
    LabelStyle -> 22, LegendMarkers -> markers1, 
    LegendMarkerSize -> 14], {Left, Bottom}]]

The output I am getting is

enter image description here

I want the plot legend to include line type in it as follows

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ I think you want LineLegend with the appropriate LegendMarkers setting instead. Alternatively, just PlotLegends->Placed[{"a","b",...},...] might already be enough $\endgroup$
    – Lukas Lang
    Commented May 24, 2021 at 16:50
  • $\begingroup$ This I have tried, but it was not giving the required result. $\endgroup$ Commented May 27, 2021 at 8:14
  • $\begingroup$ try LineLegend[{"1", " 2", " 3", "4"}, LabelStyle -> 22, LegendMarkerSize -> {50, 14}, LegendMarkers -> markers1] or LineLegend[{"1", " 2", " 3", "4"}, LabelStyle -> 22, LegendMarkerSize -> {50, 14}]? $\endgroup$
    – kglr
    Commented Apr 29, 2023 at 16:33

1 Answer 1

1
$\begingroup$
data = {{{0, 1}, {2, 2}, {4, 3}, {6, 4}, {8, 4}, {10, 3}, {12, 
     6}, {14, 2}, {16, 3}, {18, 4}, {20, 8}}, {{0, 5}, {2, 4}, {4, 
     3}, {6, 8}, {8, 2}, {10, 6}, {12, 1}, {14, 3}, {16, 5}, {18, 
     1}, {20, 2}}, {{0, 9}, {2, 8}, {4, 4}, {6, 8}, {8, 5}, {10, 
     7}, {12, 1}, {14, 0}, {16, 0}, {18, 5}, {20, 4}}, {{0, 3}, {2, 
     8}, {4, 2}, {6, 1}, {8, 2}, {10, 9}, {12, 0}, {14, 8}, {16, 
     5}, {18, 4}, {20, 7}}};
Xgrid = Range[0, 20, 1];
Ygrid = Range[0, 150, 5];
Ygrid1 = Range[0, 300, 10];
markers1 = {Graphics[Rectangle[], ImageSize -> 12], 
   Graphics[Rectangle[], ImageSize -> 12], 
   Graphics[Disk[], ImageSize -> 14], 
   Graphics[Disk[], ImageSize -> 14]};
style = {Directive[Red, Thick], Directive[Red, Thick, Dashed], 
   Directive[Blue, Thick], Directive[Blue, Thick, Dashed]};

ListPlot[data
 , PlotStyle -> style
 , Frame -> True
 , Joined -> True
 , PlotMarkers -> markers1
 , PlotRange -> All
 , ImageSize -> 800
 , PlotLabel -> Style["m", Black, 32]
 , FrameStyle -> Directive[Black, 28, Thick]
 , FrameLabel -> {"a", "b"}
 , PlotLegends -> Placed[
   LineLegend[
    Style[#, 24, Bold] & /@ {1, 2, 3, 4}
    , LegendFunction -> "Frame"
    ], {0.85, .8}]]

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.