0

I'm trying to reproduce the following pyplot figure with pgfplots:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1)
plt.plot(np.log(x)-np.log(1-x), np.log(x / (x + (1-x))))
plt.xlim(-3, 3)
plt.show()

My attempt so far with pgfplots, using x coord trafo:

\documentclass[tikz,12pt,preview]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.13} 

\begin{document}

\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=0:1,samples=200},
  x coord trafo/.code={\pgfmathparse{log10(#1) - log10(1-#1)}},
  ]
  \addplot {log10(x / (x + (1-x)))};
\end{axis}
\end{tikzpicture}

\end{document}

I think I'm close, but am unable to set xlim to (-3, 3). I think I'm thinking too much in a pyplot manner! Is there an elegant way to achieve this?

  • I don't know pyplot, but are you searching for xmin and xmax? – Stefan Pinnow Jul 29 at 18:36
2

Hopefully everything is stated in the comments of the code ...

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % set the x axis limits
        % (for details see also the comment before `\addplot`)
        xmin=-3,xmax=3,
%        % commented `no markers` to show that default sample rate is
%        % sufficient when the domain is adapted accordingly
%        no markers,
        % because at x = 0 and x = 1 the values are not defined
        % I adopted the values a bit
        domain=0.001:0.999,
%        % because after adopting the `domain` the first and last point
%        % aren't "skipped" there should be no need for such a high sample rate
%        samples=51,
        % added `smooth` just in case the `samples` are to less
        smooth,
    ]
        % instead of using `x coord trafo` just plot a parametric plot
        % (as I assume you did in pyplot)
        % this allows to easily set `xmin` and `xmax`
        % (if you would stick to `x coord trafo` you would need to know
        %  the *untransformed* x values which correspond to the
        %  transformed values of -3 and 3 and state them in `xmin` and `xmax`)
        \addplot (
            {log10(x) - log10(1-x)},
            {log10(x / (x + (1-x)))}
        );
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Thanks Stefan! Oddly I did not realise I could do a parametric plot...! Thanks very much for the comments in the code. – jodles Jul 29 at 19:43
1

Why don't you use a parametric plot?

\documentclass[tikz,12pt,preview]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13} 

\begin{document}

\begin{tikzpicture}
    \begin{axis}[domain=0.0:1,samples=200,]
        \addplot[thick, blue] ({log10(x)-log10(1-x)}, {log10(x / (x + (1-x)))});
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks Rmano! It baffles me that I did not realise I could use a parametric plot... – jodles Jul 29 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.