Tikz for Gaussian Function: Enhance Your Figures with Custom Commands

In summary: I will try thatIn summary, the conversation discusses how to include a TikZ picture in a document and how to add new commands and packages to the preamble. The specific example of a Gaussian function is used to demonstrate how to plot a graph and fill a certain area under the curve. The conversation also mentions a template and various tricks for working with TikZ.
  • #1
karush
Gold Member
MHB
3,269
5
\documentclass{article}
\usepackage{pgfplots}
\begin{document}

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

\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}
\end{tikzpicture}
\end{document}

ok I don't think MHB will process a newcommand but I don't know how to put this in the after \begin{tikzpicture} line
the problem with posting pic here is eventually they get remove and OP is useless...

this tikz code renders in overleaf but I also have many newcommands in preamble
 
Physics news on Phys.org
  • #2
The TikZ picture must be a fragment that starts with \begin{tikzpicture} and ends with \end{tikzpicture}.
Any other commands that are needed must be inside this fragment.
That is in particular \usepackage{pgfplots} and the \newcommand.
And since latex requires that \usepackage{pgfplots} is in the document preamble, it must be prefixed with %preamble.

The result is:

Code:
\begin{tikzpicture}

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

\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}

\end{tikzpicture}

\begin{tikzpicture}

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

\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}

\end{tikzpicture}
 
Last edited:
  • #3
Klaas van Aarsen said:
The TikZ picture must be a fragment the starts with \begin{tikzpicture} and ends with \end{tikzpicture}.
Any other commands that are needed must be inside this fragment.
That is in particular \usepackage{pgfplots} and the \newcommand.
And since latex requires that \usepackage{pgfplots} is in the document preamble, it must be prefixed with %preamble.

The result is:

Code:
\begin{tikzpicture}

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

\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}

\end{tikzpicture}

\begin{tikzpicture}

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

\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}

\end{tikzpicture}

ok that helps a lot
pretty new with tikz but feel I should pursue it
I thot % was a blocker
 
  • #4
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))} % Gauss function, parameters mu and sigma
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-4:4,samples=50,smooth}, % All plots: from -4:4, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
%\addplot {\gauss{0}{0.5}};
%\addplot {\gauss{1}{0.75}};
\addplot {\gauss{0}{1}};

\end{axis}

\end{tikzpicture}

So how do I put in a fill of $P(z \le 1.75)$
I tried various lines but just got error
also I don't need the y axis
 
  • #5
karush said:
So how do I put in a fill of $P(z \le 1.75)$
I tried various lines but just got error
also I don't need the y axis
There is a template in the https://tikzimages.mathhelpboards.com/tikz/tikzlive.html that shows how to fill a part under the Guassian graph.
That template also shows a couple more tricks.
More specifically, it shows that we can use \addplot [fill=cyan, domain=-4:1.75] {\gauss{0}{1}} \closedcycle.

However, you currently have a domain that applies to every plot and takes precedence.
That domain needs to be set as default instead of forcefully for every plot, which we can do by specifying it outside the style for every plot.

We can remove the y-axis with axis y line=none.

The result is:
Code:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))} % Gauss function, parameters mu and sigma
\begin{axis}[every axis plot post/.append style={
  mark=none,samples=50,smooth}, % All plots: 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x axis
axis y line=none, % the * suppresses the arrow tips
enlargelimits=upper, % extend the axes a bit to the right and top
domain=-4:4] % Default for all plots: from -4:4

\addplot [fill=cyan!30, draw=none, domain=-4:1.75] {\gauss{0}{1}} \closedcycle;

%\addplot {\gauss{0}{0.5}};
%\addplot {\gauss{1}{0.75}};
\addplot {\gauss{0}{1}};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))} % Gauss function, parameters mu and sigma
\begin{axis}[every axis plot post/.append style={
mark=none,samples=50,smooth}, % All plots: 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x axis
axis y line=none, % the * suppresses the arrow tips
enlargelimits=upper, % extend the axes a bit to the right and top
domain=-4:4] % Default for all plots: from -4:4

\addplot [fill=cyan!30, draw=none, domain=-4:1.75] {\gauss{0}{1}} \closedcycle;

%\addplot {\gauss{0}{0.5}};
%\addplot {\gauss{1}{0.75}};
\addplot {\gauss{0}{1}};
\end{axis}
\end{tikzpicture}
 
  • #6
ok mahalo much
 

Similar threads

Replies
7
Views
1K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
1
Views
5K
Replies
8
Views
2K
Replies
4
Views
2K
Back
Top