2

The following is a MWE I made to test the use of \pgftransformnonlinear on a pgfplot (via TikZ's axis environment). It uses the following map: enter image description here. (based on the answer here: Nonlinear transformation of tikzpicture according to a function)

In principal this works ok, but it doesn't really use the coordinates of the axis environment itself. Foe example, one can clearly see that the transformation isn't symmetric across the y axis as one would expect. I think this is because \pgftransformnonlinear works in a completely different coordiante system than that of the axis environment.

So my question is as follows: is there a way to apply non-linear transformations such that they use the coordinates of the internal axis environment?

Code:

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfmodule{nonlineartransformations}
\pgfplotsset{compat=1.16}
\pgfkeys{
    /pgfplots/vector plane/.style={
    },
}
\makeatletter
\def\mytransformation{%
    \pgfmathsetmacro{\myX}{\pgf@x}
    \pgfmathsetmacro{\myY}{0.001*\pgf@x*\pgf@x+\pgf@y}
    \setlength{\pgf@x}{\myX pt}
    \setlength{\pgf@y}{\myY pt}
}
\makeatother

\begin{document} \begin{figure} \centering \begin{tikzpicture} \begin{scope} \pgftransformnonlinear{\mytransformation} \begin{axis}[ width=10cm, height=10cm, axis x line=middle, axis y line=middle, xlabel=$x$, ylabel=$y$, every axis x label/.style={ at={(ticklabel* cs:1.02)}, anchor=west, }, every axis y label/.style={ at={(ticklabel* cs:1.02)}, anchor=south, }, axis line style={stealth-stealth, thick}, label style={font=\large}, tick label style={font=\large}, xticklabels={,,}, yticklabels={,,}, samples=100, xmin=-10, xmax=10, ymin=-10, ymax=10, grid=both, major grid style={black!5}, minor grid style={black!5}, ] \end{axis} \end{scope} \end{tikzpicture} \end{figure} \end{document}

Result:

The figure generated by the above code

pelegs
  • 267

1 Answers1

1

Your transformation seems to based on an axis origin at (0,0), which may be different than pgf origin (here at bottom left).

One solution would be to place the axis at (0,0) of the pgf (which is also the default behavior), but change the anchor of the axis to origin. Origin is a special anchor for the axis. See pgfplots manual for other anchor options.

\begin{axis}[
    at = {(0,0)},
    anchor = {origin},

You can, of course omitt the at = {(0,0)}, as it default position.

enter image description here

Full code:

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfmodule{nonlineartransformations}
\pgfplotsset{compat=1.16}
\pgfkeys{
    /pgfplots/vector plane/.style={
    },
}
\makeatletter
\def\mytransformation{%
    \pgfmathsetmacro{\myX}{\pgf@x}
    \pgfmathsetmacro{\myY}{0.001*\pgf@x*\pgf@x+\pgf@y}
    \setlength{\pgf@x}{\myX pt}
    \setlength{\pgf@y}{\myY pt}
}
\makeatother

\begin{document} \begin{figure} \centering \begin{tikzpicture} \begin{scope} \pgftransformnonlinear{\mytransformation} \begin{axis}[ at = {(0,0)}, anchor = {origin}, width=10cm, height=10cm, axis x line=middle, axis y line=middle, xlabel=$x$, ylabel=$y$, every axis x label/.style={ at={(ticklabel* cs:1.02)}, anchor=west, }, every axis y label/.style={ at={(ticklabel* cs:1.02)}, anchor=south, }, axis line style={stealth-stealth, thick}, label style={font=\large}, tick label style={font=\large}, xticklabels={,,}, yticklabels={,,}, samples=100, xmin=-10, xmax=10, ymin=-10, ymax=10, grid=both, major grid style={black!5}, minor grid style={black!5}, ] \end{axis} \end{scope} \end{tikzpicture} \end{figure} \end{document}

Hasan Zakeri
  • 1,942