[Tex/LaTex] Histogram with overlaying Gauss curve (pgfplots)

pgfplots

I am struggling to create a histogram with an overlaying normal (Gauss) curve using PGFplots. To be more specific, I want to recreate this SPSS plot:

Histogram with Gauss curve overlay (SPSS)

Creating the histogram using PGFplots is no problem for me. The plot looks like this:

enter image description here

The code for the plot above is:

% Gauss function, parameters mu and sigma
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}

% Histogram
\begin{tikzpicture}
    \begin{axis}[
        height=7cm,
        width=6cm,
        xmin=0.67,
        xmax=0.75,
    ]

    \addplot[
        black,
        fill=lightgray,
        hist,
        hist/bins=20,
    ] table[
        y=true-error,
    ] {Data/compare-cv-normality-1002-error.dat};

    %\addplot {\gauss{0.71}{0.00944}};

    \end{axis}
\end{tikzpicture}

As you can see, I already have the code for the Gauss plot inserted, but is commented because it produces the following errors:

Dimension too large. ^^I^^I\addplot {\gauss{0.71}{0.00944}};
Arithmetic overflow. ^^I^^I\addplot {\gauss{0.71}{0.00944}};

Also, the second problem is that adding the Gauss sets the X axis to (-5, 5) range and I have to manually readjust it according to the histogram data. So far, this is my least priority as the solution is quite trivial. But if you could solve it without setting the xmin and xmax, I would appreciate it.

Contents of the data file:

true-error
0.6949672084402624
0.7182777302537782
0.7026660963786713
0.7184915882520673
0.7052323923581408
0.7164955802680354
0.7028086683775306
0.6941117764471058
0.7085828343313373
0.7038779583689764
0.7040205303678357
0.7072996863416026
0.6899059024807528
0.7060878243512974
0.7093669803250642
0.7167094382663245
0.7295409181636726
0.7264043341887654
0.7033076703735386
0.7170658682634731
0.7255489021956087
0.7386655260906758
0.7051611063587111
0.708654120330767
0.6983889364128885
0.7140718562874252
0.6916880524664956
0.6906187624750499
0.7093669803250642
0.6945394924436841
0.7037353863701169
0.7087254063301968
0.7050185343598517
0.7074422583404619
0.6953236384374109
0.721271742229826
0.7040205303678357
0.7088679783290561
0.710151126318791
0.7279726261762189
0.6976047904191617
0.7176361562589108
0.7006700883946393
0.7020245223838039
0.7036641003706872
0.7343170801254634
0.6995295124037639
0.7070145423438836
0.714357000285144
0.7197034502423724
0.712360992301112
0.7179213002566296
0.7147134302822925
0.7077274023381808
0.7339606501283148
0.7131451382948389
0.7033076703735386
0.7085115483319077
0.7072284003421728
0.7236954662104362
0.7182777302537782
0.6976047904191617
0.7201311662389507
0.7147847162817222
0.710151126318791
0.7091531223267751
0.7107214143142286
0.6983176504134588
0.7212004562303963
0.7191331622469347
0.7112204163102367
0.7098659823210721
0.7115768463073853
0.7137867122897062
0.7288280581693756
0.6959652124322783
0.7135728542914171
0.711149130310807
0.7217707442258341
0.7013116623895067
0.7173510122611919
0.7087966923296265
0.7050898203592815
0.7058026803535785
0.7099372683205019
0.703236384374109
0.7147847162817222
0.7126461362988309
0.7068719703450242
0.7081551183347591
0.7308953521528372
0.7181351582549187
0.7228400342172797
0.7112917023096664
0.7136441402908469
0.7160678642714571
0.7192757342457942
0.6962503564299971
0.7252637581978899
0.7192757342457942
0.7162817222697462
0.6963929284288566
0.7040918163672655
0.7163530082691759
0.7053036783575706
0.7162104362703166
0.7241944682064443
0.7011690903906472
0.7122184203022527
0.7309666381522669
0.6975335044197319
0.7082264043341888
0.7154975762760194
0.7150698602794411
0.7130025662959795
0.7132164242942686
0.7053036783575706
0.7136441402908469
0.7137867122897062
0.6946820644425434
0.7073709723410322
0.7005275163957798
0.7144995722840034
0.7177074422583405
0.7015255203877958
0.7055888223552894
0.7150698602794411
0.7087966923296265
0.6940404904476761
0.7142857142857143
0.7060165383518677
0.7038779583689764
0.7013829483889364
0.710151126318791
0.7045908183632734
0.7305389221556886
0.7202024522383804
0.7308240661534074
0.6928286284573709
0.7033076703735386
0.7010265183917879
0.6926147704590818
0.704947248360422
0.7080838323353293
0.7023809523809523
0.7091531223267751
0.6906187624750499
0.7276161961790705
0.6978899344168805
0.7116481323068149
0.7124322783005418
0.7127174222982606
0.7120045623039635
0.7170658682634731
0.7145708582834331
0.7043056743655546
0.7261904761904762
0.7090105503279156
0.7330339321357285
0.6991017964071856
0.7102224123182207
0.7092244083262047
0.7230538922155688
0.7092244083262047
0.7028086683775306
0.7126461362988309
0.6912603364699174
0.7192044482463644
0.7198460222412318
0.7065155403478757
0.7256914741944682
0.7264043341887654
0.7035215283718278
0.7035215283718278
0.6946820644425434
0.6966780724265754
0.7151411462788708
0.7033076703735386
0.7033076703735386
0.6963929284288566
0.7051611063587111
0.717564870259481
0.7145708582834331
0.7266894781864842
0.7083689763330482
0.7100798403193613
0.6884088964927289
0.720558882235529
0.7057313943541489
0.721271742229826
0.7100798403193613
0.7154262902765897
0.7248360422013117
0.7048759623609923
0.7043769603649843
0.7093669803250642
0.70544625035643
0.7154975762760194
0.7004562303963502
0.7140718562874252
0.7122897063016823
0.7018819503849444
0.7177787282577702
0.7241944682064443
0.7119332763045337
0.721271742229826
0.7068719703450242
0.7080838323353293
0.7015255203877958
0.7224123182207014
0.6894069004847448
0.7082976903336184
0.7021670943826632
0.7036641003706872
0.6962503564299971
0.7312517821499858
0.7070858283433133
0.6966780724265754
0.6976760764185914
0.7107214143142286
0.7184915882520673
0.7045908183632734
0.7110778443113772
0.7020958083832335
0.7107214143142286
0.7188480182492158
0.7045195323638438
0.7142857142857143
0.7141431422868548
0.6963216424294268
0.714357000285144
0.7068006843455945
0.7263330481893356
0.7120045623039635
0.7217707442258341
0.7058739663530083
0.703949244368406
0.7107927003136584
0.7087254063301968
0.7266181921870545
0.7154975762760194
0.7122897063016823
0.7058739663530083
0.7162817222697462
0.6963216424294268
0.7077274023381808
0.7130025662959795
0.7154262902765897
0.7001710863986313
0.7090818363273453
0.7180638722554891
0.7191331622469347
0.7067293983461648
0.7306814941545481
0.7196321642429427
0.6998859424009125
0.7058739663530083
0.7125748502994012
0.6999572284003421
0.702238380382093
0.7087966923296265
0.7234816082121471
0.7078699743370402
0.7035215283718278
0.7196321642429427
0.6994582264043342
0.7085828343313373
0.7129312802965497
0.7088679783290561
0.7027373823781009
0.7045195323638438
0.6901910464784716
0.6917593384659253
0.7048759623609923
0.7178500142571999
0.7060165383518677
0.712360992301112
0.7139292842885657
0.7186341602509267
0.7132164242942686
0.7204163102366695
0.7162104362703166
0.7064442543484459
0.7087966923296265
0.6957513544339892
0.7223410322212718
0.7160678642714571
0.7100798403193613
0.708654120330767
0.6921870544625036
0.7060165383518677
0.7194895922440833
0.7083689763330482
0.7115055603079555
0.6993156544054747
0.7088679783290561
0.7135728542914171
0.6984602224123182
0.716566866267465
0.7058739663530083
0.7050185343598517
0.6947533504419732
0.7115768463073853
0.7251924721984602
0.7058026803535785
0.7108639863130881
0.7124322783005418
0.7026660963786713
0.713857998289136
0.7191331622469347
0.710151126318791
0.7166381522668948
0.7045195323638438
0.703949244368406
0.6985315084117479
0.722269746221842
0.7010978043912176
0.7134302822925578
0.6940404904476761
0.7070145423438836
0.699743370402053
0.7107214143142286
0.7060165383518677
0.7189193042486456
0.714856002281152
0.7077274023381808
0.7159252922725977
0.7064442543484459
0.6927573424579413
0.7092244083262047
0.7199885942400912
0.7074422583404619
0.7100798403193613
0.7091531223267751
0.7135015682919874
0.7203450242372398
0.711149130310807
0.7239806102081551
0.7174935842600513
0.709652124322783
0.7125035642999715
0.7131451382948389
0.717564870259481
0.6930424864556601
0.7090818363273453
0.7053036783575706
0.7095808383233533
0.7147134302822925
0.6940404904476761
0.7105075563159395
0.7281151981750784
0.7221271742229826
0.7065155403478757
0.6946107784431138
0.7147134302822925
0.7093669803250642
0.699743370402053
0.7102936983176504
0.72276874821785
0.7015968063872255
0.7241944682064443
0.7233390362132877
0.7060165383518677
0.7286142001710864
0.7095808383233533
0.7183490162532079
0.7079412603364699
0.7235528942115769
0.7000285143997719
0.7115768463073853
0.7140005702879955
0.6976760764185914
0.681708012546336
0.7013829483889364
0.7050898203592815
0.7063016823495866
0.691830624465355
0.7107927003136584
0.7005988023952096
0.7142144282862846
0.7097234103222128
0.7135728542914171
0.710151126318791
0.7172084402623324
0.7219133162246935
0.7063016823495866
0.7174222982606216
0.7132877102936983
0.6928999144568007
0.7144995722840034
0.7110778443113772
0.7177787282577702
0.7107927003136584
0.7179925862560593
0.7095095523239235
0.689620758483034
0.7142144282862846
0.7107214143142286
0.7025948103792415
0.6936840604505276
0.7260479041916168
0.7145708582834331
0.7122897063016823
0.7105075563159395
0.6993869404049045
0.710151126318791
0.7042343883661248
0.7105788423153693
0.7061591103507271
0.7125748502994012
0.7114342743085258
0.7210578842315369
0.7204875962360993
0.7275449101796407
0.7040918163672655
0.7063729683490163
0.7080125463358996
0.6990305104077559
0.7194183062446535
0.698745366410037
0.72276874821785
0.7082264043341888
0.7063016823495866
0.7085828343313373
0.702238380382093
0.7142144282862846
0.7090105503279156
0.7166381522668948
0.7109352723125179
0.7169945822640433
0.7068006843455945
0.7082264043341888
0.706943256344454
0.7104362703165098
0.7050898203592815
0.6847020245223838
0.6991730824066154
0.6942543484459652
0.7058026803535785
0.7137154262902766
0.7097946963216424
0.6958939264328486
0.710151126318791
0.7042343883661248
0.7105075563159395
0.7079412603364699
0.7181351582549187
0.704947248360422
0.7219133162246935
0.7088679783290561
0.7261904761904762
0.7036641003706872
0.7151411462788708
0.7011690903906472
0.7003849443969205
0.6991017964071856
0.6938979184488167
0.7014542343883661
0.7035215283718278
0.72027373823781
0.7102936983176504
0.7075135443398917
0.7152837182777303
0.7164955802680354
0.7188480182492158
0.7166381522668948
0.707656116338751
0.7134302822925578
0.7023809523809523
0.7053749643570003
0.7065155403478757
0.7304676361562589
0.710151126318791
0.7155688622754491
0.7119332763045337
0.7109352723125179
0.7082264043341888
0.701739378386085
0.7071571143427431
0.7256914741944682
0.7070145423438836
0.7055175363558597
0.7187767322497861
0.713857998289136
0.7149272882805817
0.7035215283718278
0.7107214143142286
0.7030938123752495
0.7033789563729683
0.714357000285144
0.7035215283718278
0.7242657542058739
0.7176361562589108
0.7080838323353293
0.7230538922155688
0.725762760193898
0.6946107784431138
0.7000998003992016
0.7139292842885657
0.7149985742800115
0.7189905902480753
0.7301112061591104
0.720558882235529
0.6894781864841745
0.7241231822070145
0.6998146564014828
0.7192757342457942
0.7030225263758197
0.7023096663815227
0.7156401482748788
0.7189905902480753
0.715854006273168
0.7100798403193613
0.7154975762760194
0.6869831765041345
0.7132877102936983
0.7160678642714571
0.6914741944682065
0.7119332763045337
0.7154975762760194
0.7060878243512974
0.6975335044197319
0.7080838323353293
0.715854006273168
0.7070145423438836
0.6973196464214428
0.7072284003421728
0.7025235243798118
0.7098659823210721
0.7041631023666952
0.7242657542058739
0.7136441402908469
0.7193470202452238
0.7000285143997719
0.7172797262617622
0.7087254063301968
0.7178500142571999
0.7050898203592815
0.712360992301112
0.7187054462503565
0.713857998289136
0.7114342743085258
0.7289706301682349
0.7087254063301968
0.7210578842315369
0.7261904761904762
0.708654120330767
0.7107214143142286
0.716852010265184
0.7187054462503565
0.721271742229826
0.7063016823495866
0.7209153122326775
0.7203450242372398
0.7033076703735386
0.7024522383803821
0.7058026803535785
0.7125035642999715
0.7221984602224123
0.7130738522954092
0.7179213002566296
0.7192757342457942
0.710151126318791
0.7075135443398917
0.6963929284288566
0.6976047904191617
0.702238380382093
0.7050185343598517
0.7120045623039635
0.7129312802965497
0.701739378386085
0.7187767322497861
0.705945252352438
0.7127174222982606
0.7031650983746792
0.7242657542058739
0.7142144282862846
0.7065868263473054
0.7043769603649843
0.710151126318791
0.7258340461933276
0.7142144282862846
0.7094382663244939
0.7217707442258341
0.7223410322212718
0.7129312802965497
0.727473624180211
0.7008839463929284
0.7048759623609923
0.7229113202167095
0.7092244083262047
0.7113629883090961
0.727473624180211
0.6993869404049045
0.7014542343883661
0.7095808383233533
0.7034502423723981
0.6929712004562304
0.7174935842600513
0.7145708582834331
0.723766752209866
0.7066581123467351
0.7131451382948389
0.6974622184203022
0.7212004562303963
0.713857998289136
0.6910464784716281
0.7113629883090961
0.6951097804391217
0.7189193042486456
0.7155688622754491
0.6901197604790419
0.7087254063301968
0.6976760764185914
0.7132877102936983
0.7109352723125179
0.7177074422583405
0.7149272882805817
0.7092244083262047
0.7087966923296265
0.7075848303393214
0.7047333903621329
0.706943256344454
0.7015968063872255
0.7187767322497861
0.6959652124322783
0.7207014542343884
0.7098659823210721
0.7082264043341888
0.7079412603364699
0.7097234103222128
0.7149985742800115
0.712147134302823
0.7221271742229826
0.725762760193898
0.7070145423438836
0.7137867122897062
0.6939692044482464
0.7048046763615626
0.7335329341317365
0.7018819503849444
0.704947248360422
0.7242657542058739
0.7213430282292558
0.7224836042201311
0.726475620188195
0.7169945822640433
0.7125748502994012
0.7135015682919874
0.7115768463073853
0.7040918163672655
0.6873396065012831
0.705945252352438
0.7189905902480753
0.7229113202167095
0.7129312802965497
0.7259053321927573
0.7221984602224123
0.7191331622469347
0.7047333903621329
0.7243370402053037
0.7197034502423724
0.7251924721984602
0.7085115483319077
0.724764756201882
0.6929712004562304
0.7116481323068149
0.7132164242942686
0.7082976903336184
0.7139292842885657
0.7107214143142286
0.7162104362703166
0.7183490162532079
0.7083689763330482
0.7052323923581408
0.6896920444824637
0.7179213002566296
0.7098659823210721
0.7112204163102367
0.7199885942400912
0.7083689763330482
0.7259053321927573
0.6996007984031936
0.7142144282862846
0.7019532363843741
0.7187767322497861
0.7178500142571999
0.7129312802965497
0.6832763045337895
0.7184915882520673
0.7025235243798118
0.7108639863130881
0.7155688622754491
0.7077986883376105
0.7191331622469347
0.712360992301112
0.7270459081836327
0.7090818363273453
0.7144282862845737
0.7136441402908469
0.7110065583119475
0.7152124322783006
0.7313230681494155
0.7087254063301968
0.7077274023381808
0.7092244083262047
0.7100798403193613
0.7110778443113772
0.7164242942686057
0.7268320501853436
0.7125748502994012
0.7243370402053037
0.7174935842600513
0.6928999144568007
0.7333903621328771
0.7098659823210721
0.7020958083832335
0.6926147704590818
0.7120045623039635
0.7135015682919874
0.7162817222697462
0.7179213002566296
0.7260479041916168
0.7132164242942686
0.6978899344168805
0.7234103222127174
0.7105075563159395
0.7130025662959795
0.7187054462503565
0.7078699743370402
0.736883376104933
0.7281151981750784
0.7051611063587111
0.7014542343883661
0.7179213002566296
0.7010978043912176
0.7060165383518677
0.7135728542914171
0.7068006843455945
0.7313943541488451
0.691331622469347
0.7129312802965497
0.7173510122611919
0.7085115483319077
0.7015255203877958
0.7066581123467351
0.7189905902480753
0.7108639863130881
0.6852723125178215
0.7137867122897062
0.7154975762760194
0.72027373823781
0.7135015682919874
0.7153550042771599
0.7163530082691759
0.7081551183347591
0.7162817222697462
0.7137867122897062
0.7071571143427431
0.7268320501853436
0.7003136583974907
0.7119332763045337
0.7137867122897062
0.7035215283718278
0.6979612204163103
0.7008839463929284
0.7163530082691759
0.6926860564585116
0.7048046763615626
0.7208440262332478
0.7046621043627032
0.7027373823781009
0.7196321642429427
0.6925434844596521
0.6953236384374109
0.7135015682919874
0.7254063301967494
0.7102936983176504
0.7249786142001711
0.695038494439692
0.7221984602224123
0.7196321642429427
0.7091531223267751
0.6992443684060451
0.7225548902195609
0.7060878243512974
0.7154975762760194
0.7108639863130881
0.7216994582264044
0.7070145423438836
0.7060878243512974
0.720558882235529
0.7209865982321072
0.7204163102366695
0.7070858283433133
0.7167094382663245
0.7261191901910464
0.6977473624180212
0.6972483604220131
0.7095095523239235
0.724764756201882
0.6998859424009125
0.7008126603934988
0.7166381522668948
0.705945252352438
0.7235528942115769
0.7058739663530083
0.7130738522954092
0.71285999429712
0.7147847162817222
0.7058739663530083
0.7223410322212718
0.7081551183347591
0.7127887082976904
0.7321072141431423
0.7157827202737382
0.713857998289136
0.7004562303963502
0.7045908183632734
0.7095095523239235
0.70544625035643
0.7153550042771599
0.6936127744510978
0.7043769603649843
0.6973909324208726
0.7258340461933276
0.7025235243798118
0.7188480182492158
0.7037353863701169
0.7105788423153693
0.7127887082976904
0.7061591103507271
0.7008126603934988
0.7077986883376105
0.7296122041631024
0.7081551183347591
0.7164242942686057
0.7109352723125179
0.6938266324493869
0.6971057884231537
0.7028086683775306
0.7053036783575706
0.7330339321357285
0.7137867122897062
0.7105075563159395
0.7225548902195609
0.7056601083547192
0.7085828343313373
0.6858426005132592
0.7045908183632734
0.7075135443398917
0.7166381522668948
0.7236954662104362
0.7209865982321072
0.7025235243798118
0.706943256344454
0.7319646421442829
0.7037353863701169
0.7072284003421728
0.7194895922440833
0.7105788423153693
0.7238380382092957
0.7063016823495866
0.7134302822925578
0.700242372398061
0.6984602224123182
0.6945394924436841
0.7171371542629028
0.6916880524664956
0.7286142001710864
0.7061591103507271
0.7092956943256344
0.705945252352438
0.7181351582549187
0.7078699743370402
0.7063016823495866
0.6883376104932991
0.7097946963216424
0.6900484744796122
0.7075135443398917
0.7189905902480753
0.6961077844311377
0.7082264043341888
0.7140005702879955
0.709652124322783
0.7179213002566296
0.7244083262047334
0.7000285143997719
0.7068006843455945
0.7075135443398917
0.7105788423153693
0.714357000285144
0.7008839463929284
0.721271742229826
0.7073709723410322
0.7055888223552894
0.7144282862845737
0.7254776161961791
0.7149985742800115
0.7040205303678357
0.7079412603364699
0.7162104362703166
0.6998146564014828
0.7078699743370402
0.7132877102936983
0.7127174222982606
0.7107214143142286
0.7072284003421728
0.7063729683490163
0.7254776161961791
0.7258340461933276
0.7147134302822925
0.7043056743655546
0.7098659823210721
0.7056601083547192
0.7093669803250642
0.7216281722269746
0.7102224123182207
0.7090105503279156
0.7075848303393214
0.6887653264898774
0.7098659823210721
0.7226974622184204
0.7116481323068149
0.70544625035643
0.6996007984031936
0.7091531223267751
0.6949672084402624
0.70544625035643
0.7197747362418021
0.7124322783005418
0.7000285143997719
0.7182777302537782
0.6981750784145994
0.7194895922440833
0.7038779583689764
0.7028799543769604
0.715854006273168
0.7314656401482749
0.7102936983176504
0.7061591103507271
0.7117907043056744
0.7161391502708868
0.7145708582834331
0.7010265183917879
0.6874108925007129
0.7241944682064443
0.7079412603364699
0.6900484744796122
0.7079412603364699
0.7202024522383804
0.6998859424009125
0.7219846022241232
0.7215568862275449
0.7055888223552894
0.7246934702024522
0.7186341602509267
0.7060878243512974
0.7174222982606216
0.7005988023952096
0.7179213002566296
0.7053749643570003
0.7005275163957798
0.7150698602794411
0.7090105503279156
0.6986740804106074
0.7217707442258341
0.7110065583119475
0.7058739663530083
0.7033789563729683
0.7374536641003707
0.7254776161961791
0.717564870259481
0.6972483604220131
0.7065155403478757
0.7057313943541489
0.7163530082691759
0.6993869404049045
0.704448246364414
0.7040205303678357
0.7130025662959795
0.7036641003706872
0.7114342743085258
0.7173510122611919
0.7127174222982606
0.7097234103222128
0.7056601083547192
0.7078699743370402
0.7089392643284859
0.7208440262332478
0.7274023381807813
0.6956087824351297
0.7030938123752495
0.7184915882520673
0.717564870259481
0.7055888223552894
0.708654120330767
0.6961790704305675
0.7224123182207014
0.7164242942686057

Best Answer

Without explicit instructions to the contrary, pgfplots is trying to evaluate the Gaussian way outside the domain you’re interested in, causing arithmetic errors as the argument of the exponential gets large.

\addplot[domain={0.67:0.75}]{\gauss{0.71}{0.00944}};

will do the trick (though of course to match the SPSS plot you probably want to include a scale parameter in the Gaussian as well).


Edit by HK

In addition to domain, to match the peak of gauss with bar, you may use ysacle=4.25. Adding samples=150 makes the gauss smoother.

 \addplot[domain={0.67:0.75},yscale=4.25,samples=150] {\gauss{0.71}{0.00944}};

gives

enter image description here


Edit by alesc

The scaling factor is calculated via the following equation:

0.997 * num_samples * (xmax - xmin) / num_bins

Provided that xmax and xmin is calculated via the +-3 sigma rule. This is also the reason for multiplying the equation with 0.997.

MWE: With x,y-labels

\documentclass[border=3mm,tikz,preview]{standalone}
\usepackage{pgfplots}

\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        height=7cm,
        width=6cm,
        xmin=0.67,
        xmax=0.75,
        xlabel = truerror,
        ylabel = Frequency
]

    \addplot[
        black,
        fill=lightgray,
        hist,
        hist/bins=20,
    ] table[
        y=true-error,
    ] {error.dat};

    \addplot[domain={0.67:0.75},yscale=4.25,samples=150] {\gauss{0.71}{0.00944}};

    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here