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
 

1. What is Tikz for Gaussian Function?

Tikz for Gaussian Function is a custom command package that allows you to easily enhance your figures by adding Gaussian functions to them. It is a powerful tool for creating visually appealing and accurate figures for scientific publications.

2. How does Tikz for Gaussian Function work?

Tikz for Gaussian Function works by using the Tikz package in LaTeX to create custom commands that can be easily inserted into your figures. These commands allow you to specify the parameters of the Gaussian function, such as its center, standard deviation, and amplitude, and then plot it onto your figure with a high degree of customization.

3. What are the advantages of using Tikz for Gaussian Function?

Tikz for Gaussian Function offers several advantages, including the ability to create highly customizable figures with precise Gaussian functions, saving time and effort compared to manually drawing them. It also allows for easy replication and modification of figures, making it ideal for scientific research and publications.

4. Can Tikz for Gaussian Function be used with other packages?

Yes, Tikz for Gaussian Function can be used in conjunction with other packages, such as PGFPlots, to create even more sophisticated figures. It can also be used with other LaTeX environments, such as Beamer, for presentations.

5. Is Tikz for Gaussian Function suitable for all levels of LaTeX users?

While some knowledge of LaTeX is helpful, Tikz for Gaussian Function is designed to be user-friendly and accessible to all levels of LaTeX users. It provides clear instructions and examples to help users get started and create professional-looking figures with ease.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
971
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
741
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
936
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
279
Back
Top