MHB Tikz for Gaussian Function: Enhance Your Figures with Custom Commands

  • Thread starter Thread starter karush
  • Start date Start date
  • Tags Tags
    Function Gaussian
AI Thread Summary
The discussion focuses on using TikZ to create Gaussian function plots in LaTeX, emphasizing the need to include necessary commands within the TikZ picture environment. Users are advised to prefix the \usepackage{pgfplots} command with %preamble since it must be in the document preamble. A specific example of defining a Gaussian function using \newcommand is provided, along with tips for customizing plots, such as filling areas under the curve and removing the y-axis. The conversation highlights the importance of setting the domain correctly for all plots and offers a solution for visualizing the probability under the Gaussian curve. Overall, the thread serves as a practical guide for enhancing figures with custom commands in TikZ.
karush
Gold Member
MHB
Messages
3,240
Reaction score
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
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:
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
 
\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
 
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}
 
ok mahalo much
 

Similar threads

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