pgfplots Histogram with Normal Curve
Variations on this question have been asked multiple times before, but I have been unsuccessful in getting them to work. I may be (and most likely am) just inept at statistics.
I wish to plot a normal curve on top of this histogram using the data contained in the CSV file.
Many answers I have seen create a normal/gauss function, but I have excluded this from the MWE in case there is another, simpler way to do it.
Any assistance would be appreciated!

MWE:
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
usepackage{pgfplotstable}
begin{document}
begin{tikzpicture}
centering
begin{axis}[
ybar interval,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
end{axis}
end{tikzpicture}
end{document}
pgfplots statistics histogram
add a comment |
Variations on this question have been asked multiple times before, but I have been unsuccessful in getting them to work. I may be (and most likely am) just inept at statistics.
I wish to plot a normal curve on top of this histogram using the data contained in the CSV file.
Many answers I have seen create a normal/gauss function, but I have excluded this from the MWE in case there is another, simpler way to do it.
Any assistance would be appreciated!

MWE:
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
usepackage{pgfplotstable}
begin{document}
begin{tikzpicture}
centering
begin{axis}[
ybar interval,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
end{axis}
end{tikzpicture}
end{document}
pgfplots statistics histogram
1
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12
add a comment |
Variations on this question have been asked multiple times before, but I have been unsuccessful in getting them to work. I may be (and most likely am) just inept at statistics.
I wish to plot a normal curve on top of this histogram using the data contained in the CSV file.
Many answers I have seen create a normal/gauss function, but I have excluded this from the MWE in case there is another, simpler way to do it.
Any assistance would be appreciated!

MWE:
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
usepackage{pgfplotstable}
begin{document}
begin{tikzpicture}
centering
begin{axis}[
ybar interval,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
end{axis}
end{tikzpicture}
end{document}
pgfplots statistics histogram
Variations on this question have been asked multiple times before, but I have been unsuccessful in getting them to work. I may be (and most likely am) just inept at statistics.
I wish to plot a normal curve on top of this histogram using the data contained in the CSV file.
Many answers I have seen create a normal/gauss function, but I have excluded this from the MWE in case there is another, simpler way to do it.
Any assistance would be appreciated!

MWE:
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
usepackage{pgfplotstable}
begin{document}
begin{tikzpicture}
centering
begin{axis}[
ybar interval,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
end{axis}
end{tikzpicture}
end{document}
pgfplots statistics histogram
pgfplots statistics histogram
asked Jan 21 at 20:22
CraigCraig
19412
19412
1
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12
add a comment |
1
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12
1
1
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12
add a comment |
1 Answer
1
active
oldest
votes
Here is a proposal building on this answer.
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
usepackage{pgfplotstable}
newcommandGetMean[2]{% from https://tex.stackexchange.com/a/268942/121799
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + thisrow{#2}}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{Sum}{pgfplotsretval}
pgfmathsetmacro{Mean}{Sum/NumRows}
}
newcommandGetVariance[3]{
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + (thisrow{#2}-#3)^2}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{SumSquares}{pgfplotsretval}
pgfmathsetmacro{Variance}{sqrt(SumSquares)/NumRows}
}
begin{document}
begin{tikzpicture}[declare function={%
gauss(x,y,z)=1/(2*z*sqrt(pi))*exp(-(x-y)^2/(2*z^2));}]
begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[ybar interval,
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
GetMean{data.csv}{A}
GetVariance{data.csv}{A}{Mean}
addplot[no marks,domain=0:8,smooth,samples=51,thick,color=blue]{Sum*gauss(x,Mean,Variance)};
end{axis}
end{tikzpicture}
end{document}

Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471197%2fpgfplots-histogram-with-normal-curve%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a proposal building on this answer.
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
usepackage{pgfplotstable}
newcommandGetMean[2]{% from https://tex.stackexchange.com/a/268942/121799
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + thisrow{#2}}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{Sum}{pgfplotsretval}
pgfmathsetmacro{Mean}{Sum/NumRows}
}
newcommandGetVariance[3]{
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + (thisrow{#2}-#3)^2}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{SumSquares}{pgfplotsretval}
pgfmathsetmacro{Variance}{sqrt(SumSquares)/NumRows}
}
begin{document}
begin{tikzpicture}[declare function={%
gauss(x,y,z)=1/(2*z*sqrt(pi))*exp(-(x-y)^2/(2*z^2));}]
begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[ybar interval,
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
GetMean{data.csv}{A}
GetVariance{data.csv}{A}{Mean}
addplot[no marks,domain=0:8,smooth,samples=51,thick,color=blue]{Sum*gauss(x,Mean,Variance)};
end{axis}
end{tikzpicture}
end{document}

Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
add a comment |
Here is a proposal building on this answer.
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
usepackage{pgfplotstable}
newcommandGetMean[2]{% from https://tex.stackexchange.com/a/268942/121799
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + thisrow{#2}}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{Sum}{pgfplotsretval}
pgfmathsetmacro{Mean}{Sum/NumRows}
}
newcommandGetVariance[3]{
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + (thisrow{#2}-#3)^2}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{SumSquares}{pgfplotsretval}
pgfmathsetmacro{Variance}{sqrt(SumSquares)/NumRows}
}
begin{document}
begin{tikzpicture}[declare function={%
gauss(x,y,z)=1/(2*z*sqrt(pi))*exp(-(x-y)^2/(2*z^2));}]
begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[ybar interval,
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
GetMean{data.csv}{A}
GetVariance{data.csv}{A}{Mean}
addplot[no marks,domain=0:8,smooth,samples=51,thick,color=blue]{Sum*gauss(x,Mean,Variance)};
end{axis}
end{tikzpicture}
end{document}

Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
add a comment |
Here is a proposal building on this answer.
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
usepackage{pgfplotstable}
newcommandGetMean[2]{% from https://tex.stackexchange.com/a/268942/121799
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + thisrow{#2}}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{Sum}{pgfplotsretval}
pgfmathsetmacro{Mean}{Sum/NumRows}
}
newcommandGetVariance[3]{
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + (thisrow{#2}-#3)^2}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{SumSquares}{pgfplotsretval}
pgfmathsetmacro{Variance}{sqrt(SumSquares)/NumRows}
}
begin{document}
begin{tikzpicture}[declare function={%
gauss(x,y,z)=1/(2*z*sqrt(pi))*exp(-(x-y)^2/(2*z^2));}]
begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[ybar interval,
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
GetMean{data.csv}{A}
GetVariance{data.csv}{A}{Mean}
addplot[no marks,domain=0:8,smooth,samples=51,thick,color=blue]{Sum*gauss(x,Mean,Variance)};
end{axis}
end{tikzpicture}
end{document}

Here is a proposal building on this answer.
documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
end{filecontents*}
usepackage{pgfplotstable}
newcommandGetMean[2]{% from https://tex.stackexchange.com/a/268942/121799
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + thisrow{#2}}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{Sum}{pgfplotsretval}
pgfmathsetmacro{Mean}{Sum/NumRows}
}
newcommandGetVariance[3]{
pgfplotstableread{#1}tableA
pgfplotstableset{
create on use/new/.style={
create col/expr={pgfmathaccuma + (thisrow{#2}-#3)^2}},
}
pgfplotstablegetrowsof{tableA}
pgfmathsetmacro{NumRows}{pgfplotsretval}
pgfplotstablegetelem{numexprNumRows-1relax}{new}of{#1}
pgfmathsetmacro{SumSquares}{pgfplotsretval}
pgfmathsetmacro{Variance}{sqrt(SumSquares)/NumRows}
}
begin{document}
begin{tikzpicture}[declare function={%
gauss(x,y,z)=1/(2*z*sqrt(pi))*exp(-(x-y)^2/(2*z^2));}]
begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
addplot +[ybar interval,
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
GetMean{data.csv}{A}
GetVariance{data.csv}{A}{Mean}
addplot[no marks,domain=0:8,smooth,samples=51,thick,color=blue]{Sum*gauss(x,Mean,Variance)};
end{axis}
end{tikzpicture}
end{document}

answered Jan 22 at 5:14
marmotmarmot
95.4k4110210
95.4k4110210
Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
add a comment |
Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
Thanks @marmot, this indeed does the trick. It has also made me realise I wasn't clear in my original question (largely because I don't know what I'm talking about). I'll post another question.
– Craig
Jan 22 at 7:22
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471197%2fpgfplots-histogram-with-normal-curve%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
When you are saying "create a normal/gauss function" do you mean literally a Gaussian whose center, width and normalization you will provide, or are you seeing a way to extract the center and the width from the data and to draw the Gaussian then?
– marmot
Jan 22 at 0:12