2
$\begingroup$

How can I include PlotLegends in this code

{data1, data2} = Transpose[Table[{2, 8/n}, {n, 20}]];

a = ListLogPlot[data1, Frame -> True, 
   PlotMarkers -> Style["\[FilledDiamond]", 10, Red]];
b = ListLogPlot[data2, Frame -> True, Joined -> True, 
   PlotStyle -> Black, Mesh -> All];

Show[a, b, PlotRange -> All, Axes -> None]

to give me a plot such the legends are like this picture

enter image description here

and

enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$

You can do all in a single ListLogPlot:

ListLogPlot[{data1, data2}, 
 Joined -> {False, True}, 
 Frame -> True, 
 PlotMarkers -> {{"◆", 10}, {"●", 10}}, 
 PlotStyle -> {Red, Black}, 
 Mesh -> All, 
 PlotLegends -> {"first", "second"}]

enter image description here

Altenatively, add the option PlotLegends in each plot. Show combines the legends automatically:

a = ListLogPlot[data1, Frame -> True, 
   PlotMarkers -> Style["◆", 10, Red], 
   PlotLegends -> {"first"}];

b = ListLogPlot[data2, Frame -> True, Joined -> True, 
   PlotStyle -> Black, Mesh -> All, 
   PlotLegends -> LineLegend[{"second"}, LegendMarkers -> {Automatic, 10}]];

Show[a, b, PlotRange -> All, Axes -> None]

![enter image description here

Update: To get a picture like the second picture in OP we can use PlotStyle. If we specify different colors for points and lines in b using, say,

   PlotStyle ->  Directive[CapForm["Round"], AbsoluteThickness[5], Black, 
     LineColor -> Blue]

the lines and markers in the legend are automatically colored to match the specification in PlotStyle.

So, we can use the first approach as follows:

ListLogPlot[{data1, data2}, Joined -> {False, True}, Frame -> True, 
 PlotMarkers -> {{"◆", 16}, {"●", 14}}, 
 PlotStyle -> {Red, Directive[CapForm["Round"], AbsoluteThickness[5], Black, 
    LineColor -> Blue]},
 Mesh -> All, PlotLegends -> {"first", "second"}]

enter image description here

And modify the second approach as follows:

a = ListLogPlot[data1, Frame -> True, 
   PlotMarkers -> Style["◆", 14, Red], 
   PlotLegends -> {"first"}];

b = ListLogPlot[data2, Frame -> True, Joined -> True, 
   PlotStyle ->  Directive[CapForm["Round"], AbsoluteThickness[5], Black, 
     LineColor -> Blue], 
   Mesh -> All, 
   MeshStyle -> AbsolutePointSize[9], 
   PlotLegends -> LineLegend[{"second"}, LegendMarkers -> {Automatic, 14}]];

Show[a, b, PlotRange -> All, Axes -> None]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you very much. I have another problem. How can I get a result like the second picture in the question? and how can I control the legend and the legendmarkers size? $\endgroup$
    – charmin
    Commented Dec 6, 2020 at 13:52
  • 1
    $\begingroup$ @charmin, please see the update. $\endgroup$
    – kglr
    Commented Dec 6, 2020 at 19:33
  • $\begingroup$ Once more, I appreciate your time and answer. $\endgroup$
    – charmin
    Commented Dec 6, 2020 at 20:06

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.