2

This problem raises a couple of issues for me. First, I am trying to place labels on a graph and I am having no success. Here is the code I've tried:

\documentclass[11pt,reqno]{amsbook}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\pgfmathdeclarefunction{gaussPDF}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-5:5,samples=50,smooth},
  axis x line*=bottom,
  xlabel=$x$,
  xlabel style={at={(axis description cs:1,0)}},
  ylabel=${f_X(x:c,d)}$,
  axis y line*=middle,
  enlargelimits=upper]
  \addplot[red] {gaussPDF(0,1)};
\end{axis}
\end{tikzpicture}

\end{document}

The xlabel style line is lifted directly from an answer to a similar question here: Positioning of Pgfplot axis labels.

My immediate problem is to place the y label on the top of the y-axis, centered and horizontal.

My more fundamental problem is that the PGFplots manual I have (v 1.4.1, 2010) doesn't help me figure these things out. On page 191 it addresses x/ylabel style ={} but I can't find a list of the keys that go in the {} or what they mean, or how to configure them. The answer in the link didn't say what 'cs' means but it did suggest that the first number following it positions the label between the origin (0) and the end of the axis (1). The second number seems to be a vertical displacement. This is a very inefficient way to figure these things out and I can't seem to parley the xlabel style line into the ylabel style line I need.

What's frustrating is that clearly many people do know these thing so it's knowable. Could someone point me at a better manual and/or a list of keys for each of the options for all of the commands that have same? I'm at the point in my work where I will need pgfplots a lot, and it will be a nightmare for me, and a monumental waste of this resource, if I'm posting questions all the time. It may just be me but I find the manual difficult to navigate, all thanks and gratitude genuinely offered to the authors.

Edit: adding a screen shot of output:

enter image description here

5
  • Can you please Edit your question and add a screenshot?
    – MS-SPO
    Aug 19, 2022 at 4:07
  • 1
    Output added. Thank you.
    – TonyK
    Aug 19, 2022 at 5:50
  • 2
    Unfortunately you refer to quite an old version of the manual. Can you please look up the reference in the latest one found here: ctan.org/pkg/pgf ? (It may also provide more answers nowadays.)
    – MS-SPO
    Aug 19, 2022 at 7:41
  • Just checking did you also refer to TikZ manual? Maybe it uses some TikZ basic that is documented there
    – user202729
    Aug 19, 2022 at 12:00
  • @user202729. Yes, but perhaps not carefully enough.
    – TonyK
    Aug 19, 2022 at 15:51

1 Answer 1

2

Like this?

enter image description here

\documentclass[border=3.141592]{standalone}
%\documentclass[11pt,reqno]{amsbook}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\pgfmathdeclarefunction{gaussPDF}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,  % <---
%
    xlabel=$x$,             xlabel style={anchor=west},
    ylabel=${f_X(x:c,d)}$,  ylabel style={anchor=south}, % <---
    enlargelimits=upper,
 %
   mark=none, domain=-5:5, samples=51, 
  ]
  \addplot [thick, red] {gaussPDF(0,1)};
\end{axis}
\end{tikzpicture}

\end{document}

(there is nothing to steal here, you can find everything in package manual ... For example see this solution.)

5
  • Exactly! And you code is a lot simpler than the code I scavenged from various posts. I take your word that everything is in the manual, but I'm finding it difficult to find what I need. For example, a search for ylabel style didn't yield anything useful. Certainly not style={anchor=south}. But I'll have another read and see how that goes.
    – TonyK
    Aug 19, 2022 at 16:01
  • @TonyK, pgfplots is based on tikz˙, so in it can use tikz` way of positioning of nodes. Axis labels are just a node and way of it positioning is described in "TikZ and & PGF" manual.
    – Zarko
    Aug 19, 2022 at 17:49
  • I didn't realize that. The code I was seeing on this Exchange for label placement, such as the code I uploaded, did not look familiar -- I hadn't seen anything like ...cs: # # in basic \draw commands. I'm used to \draw(?,?) node[left,right,above,below]{}. I'm assuming east, west, north, south play the same role. I also now realize I had an old version of the pgfpolots manual. I've just started to read the latest and perhaps I'll have better luck.
    – TonyK
    Aug 19, 2022 at 18:05
  • @TonyK, you look a very old question/answer. Meanwhile this syntax become obsolete ;-) however, it still works (due to compatibility issues). If you will update your pgfplots package than with it will be updated manual too. Recent revision is 1.18.1 (2021/05/15).
    – Zarko
    Aug 19, 2022 at 18:12
  • Thanks for all the help.
    – TonyK
    Aug 20, 2022 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .