3

I am trying to create a visual for a linear transformation done by matrices. Here is an example of what I am shooting for:

enter image description here

I would like to have the original gridlines in place, but like faded out a bit, and the newly transformed gridlines more visible.

Here is my attempt using \pgftransformcm:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[scale=1,line width=1pt]

\begin{axis}[
color= black, 
thick,
xmin=-3.9, 
xmax=3.9, 
ymin=-3.9, 
ymax=3.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\end{axis}

\begin{axis}[
color= blue, 
thick,
xmin=-3.9, 
xmax=3.9, 
ymin=-3.9, 
ymax=3.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
grid style={blue!50},
ticks=none
]

\pgftransformcm{1}{1}{0}{1}{\pgfpoint{0}{0}};

\end{axis}

\end{tikzpicture}

\end{document}

And here is my result:

enter image description here

I am very unfamiliar with \pgftransformcm, and so I am sure there is an easy fix to this that I am not seeing. It seems that for some reason the shifted gridlines are no longer centered at the origin when I use this command, which I do not want.

I also have used the axis environment only because I am used to using it for making graphs with pgfplots, and for me, having a coordinate system is much more desirable than using arbitrary points that you do without the environment (so for example, I would be able to draw a vector to the point (2,2) and know exactly where it would land). But that is just my personal preference, and if no solution is possible without removing the environment, then so be it.

I apologize if this seems like a long-winded question, but I have no idea how to approach this question. Any help would be appreciated!

akenny430
  • 1,389

1 Answers1

6

I take back what I wrote in my comment. \pgftransformcm is actually the easier option here. This code provides two ways to achieve the result.

\documentclass[border=3.14mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
 \fill[clip] (-8,-5) rectangle (8,5);
 \draw[white] (-8,-5) grid (8,5);
 \begin{scope}[x={(3,-2)},y={(2/3,7/3)}]
  \foreach \X in {-2,...,2}
  {\draw[red!30] (\X,-5) -- ++ (0,10);}
  \foreach \Y in {-4,...,4}
  {\draw[blue!30] (-3,\Y) -- ++ (6,0);}
  \draw[yellow,thick,-latex] (0,0) -- (1,0) node[above right]{$x'$};
  \draw[orange,thick,-latex] (0,0) -- (0,1) node[above left]{$y'$};
 \end{scope}
\end{tikzpicture}

\begin{tikzpicture}
 \fill[clip] (-8,-5) rectangle (8,5);
 \draw[white] (-8,-5) grid (8,5);
 \begin{scope}
  \pgftransformcm{3}{-2}{2}{1}{\pgfpoint{0cm}{0cm}}
  \foreach \X in {-2,...,2}
  {\draw[red!30] (\X,-5) -- ++ (0,10);}
  \foreach \Y in {-4,...,4}
  {\draw[blue!30] (-3,\Y) -- ++ (6,0);}
  \draw[yellow,thick,-latex] (0,0) -- (1,0) node[above right]{$x'$};
  \draw[orange,thick,-latex] (0,0) -- (0,1) node[above left]{$y'$};
 \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • Amazing answer! So is there no way to keep using the axis environment? Just curious – akenny430 Mar 29 '19 at 00:29
  • 1
    @AidenKenny Most likely there is but pgfplots does its own tricks (which, among other things, allows us to deal with very large coordinates and so on). However, judging from section 4.21 Symbolic Coordinates and User Transformations of the pgfplots manual I would assume it is nontrivial. I once tried some related things in here but gave up. –  Mar 29 '19 at 00:34
  • 1
    @AidenKenny You can install the transformations by moving the \pgftransformcm before the second axis, e.g. \pgftransformcm{3}{-2}{2}{1}{\pgfpoint{0cm}{0cm}} \begin{axis}[shift={(-3.33cm,-0.67cm)},.... in your code but I personally do not find the shift very intuitive nor pleasing. –  Mar 29 '19 at 00:47
  • Great, thank you! Also, how would I make the background white (I know the reference picture is black, I apologize for not clarifying)? – akenny430 Mar 29 '19 at 00:49
  • 1
    @AidenKenny \clip (-8,-5) rectangle (8,5); \draw (-8,-5) grid (8,5); instead of \fill[clip] (-8,-5) rectangle (8,5); \draw[white] (-8,-5) grid (8,5); should do. –  Mar 29 '19 at 00:53
  • Just curious how is the x and y were chosen (with x={(3,-2)},y={(2/3,7/3)} in the first example)? Clearly the x is the coordinate of the first vector, but I cannot see where the coordinates from y came from. – akenny430 Mar 29 '19 at 17:11
  • 1
    @AidenKenny The problem is that when you specify y, x is already installed. Call the new coordinates x' and y'. In the first step, we tell TikZ that it should use x'=(3,-2) instead of x. So far, so good. But when we tell TikZ what the new y should be, we need to give it coordinates in the basis x' and y. You can check that (2/3)*(3,-2)+(7/3)*(0,1)=(2,-4/3+7/3)=(2,1). –  Mar 29 '19 at 17:17