1
$\begingroup$

I am using ListPlot to plot many lines in the same graph. I want to put in a legend but only for a subset of these lines. For example

ListPlot[{P1,P2,P3,P4},PlotLegend -> {"a","b"}]

where P1,P2,P3,P4 are some lists that one can plot, however this creates a legend for -all- plots P1,...,P4, with empty texts for some of them. How can I make a legend that only lables two of the lines?

$\endgroup$
3
  • $\begingroup$ Perhaps Show[{ListPlot[{P1,P2}, PlotLegend->{"a", "b"}], ListPlot[{P3,P4}]}] including PlotRange if needed. $\endgroup$
    – Bill
    Oct 28, 2015 at 1:21
  • $\begingroup$ You mean PlotLegends, not PlotLegend. $\endgroup$
    – Verbeia
    Oct 28, 2015 at 1:27
  • 3
    $\begingroup$ @Verbeia: He may really be using the old PlotLegends package -- it had the syntax and behavior described. $\endgroup$ Oct 28, 2015 at 2:14

1 Answer 1

5
$\begingroup$

Works for me

{p1, p2, p3, p4} = 
 Accumulate /@ RandomVariate[NormalDistribution[0, 1], {4, 50}];


ListLinePlot[{p1, p2, p3, p4}, PlotLegends -> {"a", "b"}]

enter image description here

What version of Mathematica are you using?

ListPlot works fine, though of course by default you get dots not an unbroken line.

$\endgroup$
4
  • 1
    $\begingroup$ Same result for v9 $\endgroup$ Oct 28, 2015 at 1:51
  • $\begingroup$ Actually I was using PlotLegend, which gives the behaviour I described (and no error message). With PlotLegends it works fine now. $\endgroup$ Oct 28, 2015 at 16:02
  • 3
    $\begingroup$ But what is the syntax if he only wanted to label, for example, P1 and P4? Without using the Show function.. $\endgroup$
    – niq
    Oct 10, 2016 at 17:10
  • $\begingroup$ This is trivial. What if one wants to have the legends just for e.g. P1 and P4? $\endgroup$
    – KratosMath
    Aug 27, 2019 at 8:51

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.