4
$\begingroup$

I want to graph the first polynomials of Chebyshev almost like the graph of Wikipedia:

Chebyshev polynomials

I have tried it this way

Plot[Evaluate[Table[ChebyshevT[n - 1, x], {n, 5}]], {x, 0, 1}, 
PlotStyle -> {Blue, Orange, Green, Magenta, Purple}, 
PlotTheme -> "Scientific"]
LineLegend[{Blue, Orange, Green, Magenta, 
Purple},{"T0","T1","T2","T3","T4"}]

I can't put the legends on top of the graphic like in wikipedia or at least put them in a single line.

I have tried with the Show command but I have not succeeded. I appreciate any help.

$\endgroup$
  • 3
    $\begingroup$ Have you seen that the source Mathematica code is actually on the wikipedia image page that you linked to? $\endgroup$ – Carl Lange Feb 5 at 9:21
6
$\begingroup$

Update: A more streamlined version

Plot[Evaluate@ChebyshevT[Range[0, 4], x], {x, -1, 1},
 PlotStyle -> {Blue, Orange, Green, Magenta, Purple}, 
 Frame -> True, Axes -> False,
 GridLines -> Automatic, GridLinesStyle -> Directive[Gray, Dashed], 
 PlotLegends -> Placed[LineLegend[{"T0", "T1", "T2", "T3", "T4"}], Top]]

enter image description here

Original answer:

Plot[Evaluate[Table[ChebyshevT[n - 1, x], {n, 5}]], {x, 0, 1}, 
 PlotStyle -> {Blue, Orange, Green, Magenta, Purple}, 
 PlotTheme -> "Scientific", 
 PlotLegends -> Placed[LineLegend[{Blue, Orange, Green, Magenta, Purple}, {"T0", "T1", 
     "T2", "T3", "T4"}], Top]]

enter image description here

Also

Legended[Plot[Evaluate[Table[ChebyshevT[n - 1, x], {n, 5}]], {x, 0, 1}, 
  PlotStyle -> {Blue, Orange, Green, Magenta, Purple}, 
  PlotTheme -> "Scientific"], Placed[
  LineLegend[{Blue, Orange, Green, Magenta, Purple}, {"T0", "T1", 
    "T2", "T3", "T4"}, LegendLayout -> {"Row", 1}], Top]]

same picture

$\endgroup$
  • 3
    $\begingroup$ With a few bells: Plot[Evaluate@ Table[Tooltip[ChebyshevT[n, x]], {n, 0, 4}], {x, -1, 1}, PlotTheme -> "Scientific", FrameLabel -> (Style[#, 12, Bold] & /@ {x, ChebyshevT[n, x]}), PlotLegends -> Placed[Table[Subscript[T, n], {n, 0, 5}], Top]] $\endgroup$ – Bob Hanlon Feb 5 at 3:57
2
$\begingroup$

Here's a slight refactoring of the Wikipedia code for the image:

Inactive[Plot][
  Table[Inactive[ChebyshevT][n, x], {n, 0, 4}],
  {x, -1, 1},
  Axes -> False, GridLines -> Automatic, 
  GridLinesStyle -> Directive[LightGray, Dashed], Frame -> True, 
  PlotStyle -> AbsoluteThickness[3], 
  PlotLegends -> 
   Placed[LineLegend["Expressions", LegendMargins -> 0, 
     LegendLayout -> {"Row", 1}], Top], ImageSize -> 700] // Activate

The legend labels have the default tooltips that show that $T$ stands for ChebyshevT. The key is using Inactive to allow some parts of the code to evaluate (e.g. Table[]) but prevent other parts not to evaluate (e.g. ChebyshevT[0, x] etc.). When the plotting code is ready, the final Activate lets it run.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.