2

In the plot below, the magenta curve is drawn from the same function as the cyan curve, but the magenta curve is given in parametric form, like \addplot ({x},{f(x)}); and thus sampled differently, apparently. Increasing the number of samples helps, but the plot doesn't look right until you add a lot of samples and compile time slows way down. How can I tell pgfplots to sample appropriately for the parametric form? MWE below. A solution with \addplot gnuplot is acceptable, I couldn't figure that out either. enter image description here

\documentclass[margin=6]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xmode = log,
    ymode = log,
    axis lines = left,
    ticks=none,
    ylabel={$y_2$},
    xlabel={$y_1$},
    line width=2pt
    ]
    \addplot [domain=1e-10:1e10,magenta] ({x},{1.0/(1.0+x)});
    \addplot [domain=1e-10:1e10,cyan] {1.0/(1.0+x)};
  \end{axis}
\end{tikzpicture}

\end{document}

1 Answer 1

2

You can use samples at for that.

\documentclass[margin=6]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newcounter{iloop}
\begin{document}

\begin{tikzpicture}
  \setcounter{iloop}{-10}
  \edef\mysamples{1e-10}
  \loop
  \edef\mysamples{\mysamples,1e\number\value{iloop}}%
  \stepcounter{iloop}%
  \ifnum\value{iloop}<11\repeat
  \begin{axis}[
    xmode = log,
    ymode = log,
    axis lines = left,
    ticks=none,
    ylabel={$y_2$},
    xlabel={$y_1$},
    line width=2pt
    ]

     \addplot [magenta,samples at=\mysamples] ({x},{1.0/(1.0+x)});
     \addplot [domain=1e-10:1e10,cyan] {1.0/(1.0+x)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Here is an attempt to cast this into a style. It seems to work fine but the efforts appear high, maybe some fpu expert can cook it down to something shorter. You only need to add

log samples=between 1e-10 and 1e10 with next sample 3e-10

where the first value is the first sample, the second the last sample and the third value second sample. Please let me know if you want a different notation.

\documentclass[margin=6]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\makeatletter
\def\prepare@log@list#1#2#3{\pgfmathfloatparsenumber{#1}%
\pgfmathfloattomacro{\pgfmathresult}{\Fx}{\Mx}{\Ex}%
\pgfmathfloatparsenumber{#2}%
\pgfmathfloattomacro{\pgfmathresult}{\Fy}{\My}{\Ey}%
\pgfmathfloatparsenumber{#3}%
\pgfmathfloattomacro{\pgfmathresult}{\Fz}{\Mz}{\Ez}%
\pgfmathsetmacro{\xstart}{log10(\Mx)+\Ex}%
\pgfmathsetmacro{\xlast}{log10(\My)+\Ey}%
\pgfmathsetmacro{\xnext}{log10(\Mz)+\Ez}%
\foreach \XX [count=\YY] in {\xstart,\xnext,...,\xlast}%
{\pgfmathsetmacro{\myx}{\XX}%
\pgfmathtruncatemacro{\myy}{int(\XX)}%
\pgfmathsetmacro{\myz}{pow(10,\myx-int(\XX))}%
\ifnum\YY=1
\xdef\pgfutil@tempa{\myz e\myy}%
\else
\xdef\pgfutil@tempa{\pgfutil@tempa,\myz e\myy}%
\fi}}
\pgfplotsset{log samples/.style args={between #1 and #2 with next sample #3}{
/utils/exec=\prepare@log@list{#1}{#2}{#3},samples at=\pgfutil@tempa
}}
\makeatother
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xmode = log,
    ymode = log,
    axis lines = left,
    ticks=none,
    ylabel={$y_2$},
    xlabel={$y_1$},
    line width=2pt
    ]

     \addplot [magenta,log samples=between 1e-10 and 1e10 with next sample 3e-10] ({x},{1.0/(1.0+x)});
     \addplot [domain=1e-10:1e10,cyan] {1.0/(1.0+x)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • I was about to just \def a long string of samples on my own as the ... notation didn't work for exponentials, I wouldn't have gotten to the loop definition on my own. Thanks.
    – tsj
    Oct 31, 2019 at 15:24
  • @tsj You are welcome! One could, of course, write a style for that. Please let me know if you want this spelled out.
    – user194703
    Oct 31, 2019 at 15:27
  • Please do spell it out, I'm not sure what you refer to. And I'm also struggling a bit to produce values like 1e1.1,1e1.2, etc. in the list.
    – tsj
    Oct 31, 2019 at 15:33
  • @tsj OK, I added something.
    – user194703
    Oct 31, 2019 at 16:15

You must log in to answer this question.

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