Tikz for Gaussian Function: Enhance Your Figures with Custom Commands

  • Context: MHB 
  • Thread starter Thread starter karush
  • Start date Start date
  • Tags Tags
    Function Gaussian
Click For Summary

Discussion Overview

The discussion revolves around using TikZ to create Gaussian function plots in LaTeX, focusing on how to define custom commands and manipulate plot settings. Participants explore the syntax and structure required for effective TikZ usage, including the integration of the pgfplots package and the handling of plot domains and styles.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory
  • Homework-related

Main Points Raised

  • Some participants express uncertainty about how to include new commands within the TikZ environment, particularly regarding the placement of commands like \newcommand and \usepackage.
  • One participant provides a detailed example of a TikZ picture that includes a Gaussian function, specifying the necessary commands and settings for the plot.
  • Another participant seeks clarification on how to fill an area under the Gaussian curve and remove the y-axis, indicating they encountered errors while attempting to implement this.
  • A suggestion is made to use a specific template from an external resource that demonstrates how to fill areas under the Gaussian graph, highlighting the need to adjust the default domain for plots.
  • Participants discuss the importance of setting the domain outside of individual plot commands to avoid conflicts and ensure proper rendering.

Areas of Agreement / Disagreement

There is no consensus on the best way to implement certain features in TikZ, as participants present different approaches and solutions to similar problems. The discussion remains unresolved regarding the optimal method for filling areas under the Gaussian curve and managing plot domains.

Contextual Notes

Participants mention limitations related to the placement of commands within the TikZ environment and the necessity of adhering to LaTeX syntax rules. There are also references to specific errors encountered, which remain unaddressed in terms of resolution.

Who May Find This Useful

This discussion may be useful for individuals interested in creating complex figures using TikZ in LaTeX, particularly those working with Gaussian functions and statistical representations.

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 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
4K