0
$\begingroup$

The code below displays a histogram of the data and superimposes plots of five Weibull distributions for comparison. I have assigned colors to each Weibull curve in the distribution plots.

How can I include a legend with labels for each Weibull curve? Just to clarify, I don't really care what name is assigned to each curve shown in the legend.

data =
 {0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
  0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
  0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 
  0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
  0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
  0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 
  0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 
  0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 
  0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
  0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0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@ {WeibullDistribution[1.692103, 0.717062]}, 
 Plot[Tooltip[PDF[#, x], #], {x, 0, 2.1}, AspectRatio -> Automatic, 
    PlotStyle -> {Thick, Red}] & /@ {WeibullDistribution[1.650914, 0.715745]}, 
 Plot[Tooltip[PDF[#, x], #], {x, 0, 2.1}, AspectRatio -> Automatic, 
    PlotStyle -> {Thick, Green}] & /@ {WeibullDistribution[1.667792, 0.716308]}, 
 Plot[Tooltip[PDF[#, x], #], {x, 0, 2.1}, AspectRatio -> Automatic, 
    PlotStyle -> {Thick, Orange}] & /@ {WeibullDistribution[1.860639, 0.713741]}, 
 Plot[Tooltip[PDF[#, x], #], {x, 0, 2.1}, AspectRatio -> Automatic, 
    PlotStyle -> {Thick, Gray}] & /@ {WeibullDistribution[1.72049, 0.72098]}, 
 Frame -> {True, True, False, False}, 
 FrameLabel -> {"Wind speed (m/s)", "Probability density function "}, Axes -> False, 
 ImageSize -> 500, BaseStyle -> {FontSize -> 14}]

enter image description here

$\endgroup$
5
$\begingroup$
params =
  {{1.692103, 0.717062},
   {1.650914, 0.715745},
   {1.667792, 0.716308},
   {1.860639, 0.713741},
   {1.72049, 0.72098}};

colors = {Blue, Red, Green, Orange, Gray};

labels = {1, 2, 3, 4, 5};

Block[{x},
  Tooltip[PDF[WeibullDistribution @@ #, x], #] & /@ params //
     Plot[#, {x, 0, 2.1}, AspectRatio -> Automatic,
      PlotStyle   -> Thread[{Thick, colors}], 
      PlotLegends -> labels] &;
]

Show[Histogram[data, Automatic, "PDF"], %,
 Frame      -> {True, True, False, False},
 FrameLabel -> {"Wind speed (m/s)", "Probability density function "},
 Axes       -> False,
 ImageSize  -> 500,
 BaseStyle  -> {FontSize -> 14}
]

enter image description here

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