2
data = Flatten[Import["speedtest1B.csv"]];
distrib = HistogramDistribution[data];
histogram = Histogram[data, ChartStyle -> {Blue}];
Export["speedtest1B.pdf", 
    Overlay[{ 
    Histogram[
        data, 
        ChartStyle -> {Blue},
        ImageSize -> {500,500},
        AxesLabel -> {"start-up time [ms]", "number of samples"}
        ],
    DiscretePlot[
        CDF[distrib, x], 
        {x, 0, Max[data]},
        ImageSize -> {500,500},
        Axes -> False
        ]
        }
    ]
];

From the above I get:

enter image description here

I would like the plots to be properly aligned. Displaying the CDF x-axis on the right side of the plot would be a big plus. Also I don't realy get color-theaming so could someone please explain how to easily change the color od CDF to red.

JohnnyM
  • 489
  • 4
  • 11

2 Answers2

4
 data = 1000 RandomVariate[GammaDistribution[3, .5], 10^4];
 distrib = HistogramDistribution[data];
 Overlay[{DiscretePlot[CDF[distrib, x], {x, 0, Max[data]},
  ImageSize -> {600, 400}, Frame -> {{False, True}, {False, False}},
  FrameTicks -> {{None, Range[0.1, 1, .1]}, {None, None}},
  ImagePadding -> {{50, 100}, {15, 25}}],
 Histogram[data, ChartStyle -> Opacity[.5, Blue],
  ImageSize -> {600, 400}, ImagePadding -> {{50, 100}, {15, 25}},
  AxesLabel -> {Style["start-up time [ms]", 12],
     Style["number of samples", 12]}]},
  Alignment -> Center]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • awesome! thank you! Any idea how to label the axes? Simple AxesLabel doesnt really work. – JohnnyM Jan 30 '13 at 05:55
  • @JohnnyM, please see the update. (You need to adjust the values for the ImagePadding option to see the axes labels.) – kglr Jan 30 '13 at 06:12
  • thanks! maybe you could help with my other question: http://mathematica.stackexchange.com/questions/18733/how-to-rebuild-original-data-from-a-histogram I think it is quite basic. – JohnnyM Jan 30 '13 at 06:16
0

I know that this is a very old question, but perhaps an updated answer might make this easier for seekers such as myself who have a need to superimpose a CDF over a histogram. The following function uses SmoothedHistogram with the “PDF” option.

data=1000RandomVariate[GammaDistribution[3,0.5], 10^3];

histoCDF[data_,smHist_:True]:=Overlay[{ Histogram[data,Automatic, PlotRange->Full, ImageSize -> {600, 400}, ChartStyle->Lighter[Blue,0.8], ImagePadding -> {{50, 100}, {15, 25}}, BaseStyle->Directive[Black,14], Frame->{{True,False},{True, False}}, FrameStyle->Directive[Black,14], FrameLabel->{{"Number of Samples",None}, {"Start-up Time (min)", None}}],

SmoothHistogram[data,Automatic,"CDF", ImageSize -> {600, 400}, FrameStyle->Directive[Black,14], PlotStyle->Darker[Red,0.3], PlotRange->Full, FrameTicks->{{None,Range[0.0,1.0,0.2]}, {None,None}}, FrameLabel->{{None, "Cumulative Probability"} ,{None,None}}, Frame->{{False,True},{False,False}}, Axes->None, ImagePadding -> {{50, 100}, {15, 25}}],

If[smHist, SmoothHistogram[data,Automatic, PlotRange->Full,PlotStyle->Blue, ImageSize -> {600, 400}, Axes->None, ImagePadding -> {{50, 100}, {15, 25}}],""] }, Alignment->{Top,Center}]

Overlaying plots is now seamless, with no alignment issues. Smoothed histogram is included for comparison.

Overlay histogram with CDF

N.B. You need to use Automatic for bin specification, otherwise strange things happen with the smoothed histogram and the CDF.

GraphMan
  • 893
  • 4
  • 16
  • It's a very good idea to use SmoothHistogram but because that is a 21st century replacement for a histogram, is there a great need to show both? (Maybe if one doesn't trust smooth histograms. But if the underlying distribution is smooth, why would anyone want to see a blocky histogram?) Also, the alignment is off in your display. The smooth histogram should be right on top of the histogram. Using Show[Histogram[data, Automatic, "PDF"], SmoothHistogram[data]] is simpler and there is no misalignment. – JimB Feb 07 '22 at 05:24