3
$\begingroup$

Consider the following toy example:

With[{
  data = Table[
    Labeled[Sin[n x], n],
    {n, 1, 5}
    ]
  },
 Plot[
  data,
  {x, 0, Pi},
  PlotRange -> All
  ]
 ]

which produces the following plot: enter image description here

In cases like this, it may be hard to figure out what label corresponds to what plot. Is there an easy way to have the labels coloured consistently with the colour of the corresponding line? This would mean in this example to have the 1 coloured blue, the 2 coloured orange, etc.

$\endgroup$
2
  • $\begingroup$ In such a case a usual Legend is much better. $\endgroup$
    – corey979
    Commented May 29, 2017 at 17:27
  • $\begingroup$ @corey979 I agree for this toy example, but sometimes you have data which is well described with Labeled, except for some of the lines that overlap on the right $\endgroup$
    – glS
    Commented May 29, 2017 at 17:31

1 Answer 1

4
$\begingroup$

You can set the color of the label using Style and assign the color of the curves to be the same as the color of the labels. Further you can control the font face, boldness and size, etc...

First define a list of colors

colors = {Black, Orange, Red, Purple , Blue}

Mathematica graphics

Use colors and Style to assign the color of the text (text arbitrarily set to Bold and font size 14 as an example).

With[
 {
    data = Table[Labeled[Sin[n x],
                 Style[n, Bold, 14, colors[[n]]]], {n, 1, 5}]
  },
 Plot[data, {x, 0, Pi}, PlotRange -> All, PlotStyle -> colors]
 ]

Mathematica graphics

Update

In order to use the default colors (described here) replace colors with ColorData[97, "ColorList"]

With[
 {
  data = Table[Labeled[Sin[n x],

     Style[n, Bold, 14, ColorData[97, "ColorList"][[n]]]],
    {n, 1, 5}]
  },
 Plot[data, {x, 0, Pi}, PlotRange -> All,
  PlotStyle -> ColorData[97, "ColorList"]]
 ]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ this is the straightforward solution, yes. I was however hoping for a solution that wouldn't force me to specify my own color scheme for the plot, instead using the default one used by Mathematica for the plot $\endgroup$
    – glS
    Commented May 29, 2017 at 16:14
  • $\begingroup$ @glS Update that answers your new question. $\endgroup$ Commented May 29, 2017 at 16:48

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.