4
$\begingroup$

I want a different color for each curve that I am displaying with Show.

The functions are: custom1 custom2 custom3 custom4 custom5, and I am using the following code to plot these functions;

Show[
  Histogram[R, Automatic, "PDF"], 
  Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, 0, 1}] & /@ 
    {custom1[26.99,0.17, 13, 3.58, 0.77, 45.11], 
     custom2[152.84, 4.89, 4.91, 0.28, 172.13], 
     custom3[37.75, 0.35, 1.09, 29.49], 
     custom4[20880.9, 0.29, 21634.8],
     custom5[3.08, 5.39]}]

The graph is:

enter image description here

$\endgroup$
1

1 Answer 1

14
$\begingroup$
r = RandomVariate[NormalDistribution[0, 1], 1500];
custom = NormalDistribution[#, 1] & /@ Range[0, 2, .4]; 
(* to be replaced with your custom1, custom2, ... *)

You can do one of several things:

Make a single Plot instead of five separate ones

Show[Histogram[r, Automatic, "PDF"], 
     Plot[Evaluate[Tooltip[PDF[#, x], #] & /@ custom], {x, -3, 3}]]
(* or Plot[Tooltip[PDF[#, x], #] & /@ custom, {x, -3, 3}, Evaluated -> True] *)

enter image description here

Or provide individual colors in each of Plots:

Show[Histogram[r, Automatic, "PDF"], 
 Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, -3, 3}, 
    PlotStyle -> {Thick, Hue[RandomReal[]]}] & /@ custom]

enter image description here

Block[{i = 1}, Show[Histogram[r, Automatic, "PDF"], With[{j = i++},
     Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, -3, 3},
      PlotStyle -> {Thick, ColorData[63, "ColorList"][[j]]}]] & /@ custom]]

enter image description here

Update: add legends

Legended[Block[{i = 1}, 
  Show[Histogram[r, Automatic, "PDF"], 
   With[{j = i++}, Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, -3, 3}, 
       PlotStyle -> {Thick, ColorData[63, "ColorList"][[j]]}]] & /@ custom]], 
 LineLegend[ColorData[63, "ColorList"][[;; 5]], 
            {"custom1", "custom2", "custom3", "custom4", "custom5"}]]

enter image description here

Update 2: You can also put everything in a single Plot using the PDF of HistogramDistribution:

hd = HistogramDistribution[r];
custom2 = Prepend[custom, hd];
legendlabels = (Style[#, "Panel", 14] & /@ custom2 /. 
              DataDistribution[___] :> "HistogramDistribution[r]");

Plot[Tooltip[PDF[#, x], #] & /@ custom2, {x, -3, 3},
 LabelStyle -> {16, "Panel", Background -> None}, BaseStyle -> Thick,
 PlotStyle -> ColorData[63, "ColorList"], Evaluated -> True, Exclusions -> False, 
 FillingStyle -> (Lighter@Lighter@ColorData[63, "ColorList"][[1]]), 
 Filling -> {1 -> Axis}, PlotLegends -> legendlabels]

enter image description here

Another update: Two additional methods with minimal changes in OP's code to add random colors:

Post-process the Lines to add color directives:

op = Show[Histogram[r, Automatic, "PDF"], 
   Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, -3, 3}] & /@ custom, ImageSize -> 300];

Row[{op, op /. line : Line[__] :> {Thick, Hue[RandomReal[]], line}}, Spacer[5]]

enter image description here

Use Dynamic in PlotStyle options setting:

Show[Histogram[r, Automatic, "PDF"], 
 Plot[Evaluate[Tooltip[PDF[#, x], #]], {x, -3, 3}, 
    PlotStyle -> Dynamic[Directive[{Thick, Hue[RandomReal[]]}]]] & /@ custom, ImageSize -> 300]
(* similar picture *)
$\endgroup$
2
  • $\begingroup$ Thanks, The second works good, What about legend and line style? $\endgroup$
    – Momo
    Commented Nov 15, 2014 at 12:29
  • 1
    $\begingroup$ @Momo, please see the update re legends. $\endgroup$
    – kglr
    Commented Nov 15, 2014 at 12:50

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.