Question

I have a question with labeling a plot in Mathematica. I will describe my problem.

I have a function like this.

y = 4 x / L + 2

I want to draw a graph of y vs. x. And also,I have

L={10,20,30,40}

When I write a code like below,

Plot[y, {x, 0, 100}, 
    ImageSize -> Scaled[1.0], PlotLabel ->  Style["y vs X ", FontSize -> 18]]

I have four different plots in the same graph. I want to know how to label each plot with their relavant L value.

Was it helpful?

Solution

You can label the lines as you like using this method, based on my earlier post here. After labelling, the plot without dynamic content can be found set to plainplot.

It works by turning each line into a self-labelling button. You can modify labels for different labels.

l = {10, 20, 30, 40};
y[x_, s_] := 4 x/s + 2

plot = Plot[Evaluate@Table[y[x, u], {u, l}], {x, 0, 100},
   PlotLabel -> Style["y vs X ", FontSize -> 18]];

pos = Position[plot, _Line];
Array[(line[#] = plot[[Sequence @@ pos[[#]]]]) &, Length@l];
AddLabel[label_] := Module[{},
  AppendTo[plot[[1]], Inset[Framed[label, Background -> White], pt]];
  (* Removing buttons for final plot *)
  plainplot = plot;
  Array[
   (plainplot[[Sequence @@ pos[[#]]]] =
      plainplot[[Sequence @@ Append[pos[[#]], 1]]]) &, Length@l]]
labels = ToString /@ l;
Array[
  (plot[[Sequence @@ pos[[#]]]] =
     Button[line[#], AddLabel[labels[[#]]]]) &, Length@l];
Dynamic[EventHandler[plot,
  "MouseDown" :> (pt = MousePosition["Graphics"])]]

enter image description here

OTHER TIPS

l = {10, 20, 30, 40}
y[x_, s_] := 4 x/s + 2
<< PlotLegends`

Plot[Evaluate@Table[y[x, u], {u, l}], {x, 0, 100}, 
 ImageSize -> Scaled[1.0], 
 PlotLabel -> Style["y vs X ", FontSize -> 18], 
 PlotLegend -> ("L = " <> ToString@# & /@ l)]

Mathematica graphics

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top