4
$\begingroup$

Two lists:

line = {{0, 2.15}, {0.05, 2.03}, {0.1, 1.94}, {0.2, 1.8}, {0.3, 
1.68}, {0.4, 1.58}};
points = {{0.1009`, 1.206`}, {0.19455`, 1.4569`}, {0.3597`, 1.2315`}};

I'd like to plot the line with the points, and have a legend.

ListPlot[{line, points}, PlotRange -> {0, 2.5}, PlotLegends -> {"line", "points"}, PlotMarkers -> {" ", \[FilledSquare]}, Joined -> {True, False}]

enter image description here

Why doesn't PlotLegends pick up the line? Any way around this, other than manually?

$\endgroup$
1
  • $\begingroup$ Confused about what? I don't want markers on the first one. I want a line. I want the legend to exactly mirror what ListPlot is doing. $\endgroup$ Jul 21, 2014 at 21:35

4 Answers 4

6
$\begingroup$

This works automatically in Mathematica 10. Using your original input:

line = {{0, 2.15}, {0.05, 2.03}, {0.1, 1.94}, {0.2, 1.8}, {0.3, 
    1.68}, {0.4, 1.58}};
points = {{0.1009`, 1.206`}, {0.19455`, 1.4569`}, {0.3597`, 1.2315`}};

ListPlot[{line, points}, PlotRange -> {0, 2.5}, 
 PlotLegends -> {"line", "points"}, 
 PlotMarkers -> {" ", \[FilledSquare]}, Joined -> {True, False}]

enter image description here

$\endgroup$
3
  • $\begingroup$ (+1) Looking forward to trying it, especially to see legending and general scientific plotting. I'm especially curious whether tick marks can be customized more easily (perhaps with themes, or at least in interactive post-processing). That's just one of my eternal unresolved complaints. $\endgroup$
    – Jens
    Jul 9, 2014 at 19:17
  • $\begingroup$ You beat me to it. (+1) $\endgroup$
    – rcollyer
    Jul 9, 2014 at 19:20
  • $\begingroup$ So, yes. It wasn't functioning properly in 9. Thanks. Time to upgrade. $\endgroup$ Jul 21, 2014 at 21:39
3
$\begingroup$
line = {{0, 2.15}, {0.05, 2.03}, {0.1, 1.94}, {0.2, 1.8}, {0.3, 
    1.68}, {0.4, 1.58}};
points = {{0.1009`, 1.206`}, {0.19455`, 1.4569`}, {0.3597`, 1.2315`}};

With markers on line

ListPlot[{line, points},
 PlotRange -> {0, 2.5},
 PlotLegends -> {"line", "points"},
 PlotMarkers -> {{■, 12}, {●, 12}},
 Joined -> {True, False}]

enter image description here

Without markers on line

Show[
 ListLinePlot[line,
  PlotLegends -> {"line"},
  PlotStyle -> Blue],
 ListPlot[points,
  PlotLegends -> {"points"},
  PlotMarkers -> {{●, 12}},
  PlotStyle -> Red],
 PlotRange -> {0, 2.5},
 AxesOrigin -> {0, 0}
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ (+1) Strange that the legend for the plot with a line and marker contains only the marker and not the line, isn't it? But you do get both in the second example using ListLinePlot if PlotMarkers is added there... $\endgroup$
    – Jens
    Jul 1, 2014 at 19:11
  • $\begingroup$ Klunky, but it works. Thanks. $\endgroup$ Jul 21, 2014 at 21:36
1
$\begingroup$

It doesn't pick up the line 'cause you telling not to do ...

PlotMarkers -> {" ", ■}

with the " " you just typeset an emty space. Try

ListPlot[{line, points}, PlotRange -> {0, 2.5}, 
 PlotLegends -> {"line", "points"}, 
 PlotMarkers -> {●, ■}, 
 Joined -> {True, False}]

enter image description here

check this.

$\endgroup$
1
  • $\begingroup$ I don't want the markers. Just the line. Even with your solution, there is no line in the legend. Just the marker.. $\endgroup$ Jul 21, 2014 at 21:38
0
$\begingroup$

I get the same result as Brett Champion in version 11.3.0.0 given your code. However, I encountered a similar problem when using LogPlot, which also uses the PlotLegends option in Version 11.3.0.0.

For the function:

labeledbifurcagtingrootedtrees[x_] := Module[{m}, m = x; (Factorial[((2*m)-3)])/((2^(m-2))*Factorial[m-2])]

and its plot:

p1 = LogPlot[labeledbifurcagtingrootedtrees[n], {n, 3, 14}, PlotStyle->Blue, PlotLegends->"Labeled Unrooted Trees"]

https://i.stack.imgur.com/jCd7O.png

Note that the PlotLegend is missing.

However, simply by putting the string in a list, the problem goes away:

p1 = LogPlot[labeledbifurcagtingrootedtrees[n], {n, 3, 14}, PlotStyle->Blue, PlotLegends->{"Labeled Unrooted Trees"}]

https://i.stack.imgur.com/hZ4KS.png

This is something not expected from the Documentation for PlotLegends.

$\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.