Simplifying Tikz picture of a span

I enlarged the grid to avoid overlapping arrows.

screenshot

\documentclass[border=10mm]{standalone} 
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[thick,scale=0.5, every node/.style={scale=0.5},]
\draw[step=1cm,gray,very thin] (-2.5,-2.5) grid (2.5,2.5);
\draw[latex-latex] (-2.5,0) -- (2.5,0) node[right] {$x$-axis} ;
\draw[latex-latex] (0,-2.5) -- (0,2.5) node[above] {$y$-axis} ;

\begin{scope}[red,ultra thick]
\foreach \i in {.4,.8,1.2}
    {\foreach \j [evaluate =\j as \jj using \j*\i] in {-1,1}
{\draw [->](0,0)--(\i,\jj);
\draw [->](0,0)--(0,\jj);
\draw[->](0,0)--(-\i,\jj);
\draw[->](0,0)--(\jj,0);
}}
\end{scope}

\end{tikzpicture}
\end{document}

You could use a pic for that. I added a pic definition with which drawing the span of two vectors is as simple as saying

 \pic[red,ultra thick]{span={vector 1={(1,0)},vector 2={(0,1)}}};

This can be used as follows:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[thick,scale=0.5,nodes={scale=0.5},
    pics/span/.style={code={\tikzset{span/.cd,#1}
     \foreach \XX in {-1,0,1}
     {\foreach \YY in {-1,0,1}
     {\pgfmathtruncatemacro{\itest}{ifthenelse(\XX==0 && \YY==0,0,1)}
      \ifnum\itest=1
       \edef\temp{\noexpand\draw[pic actions,->] (0,0) -- 
       ($\XX*\pgfkeysvalueof{/tikz/span/vector 1} 
        +\YY*\pgfkeysvalueof{/tikz/span/vector 2}$);}
       \temp    
      \fi}
     }
    }},span/vector 1/.initial={(1,0)},span/vector 2/.initial={(0,1)},
    pics/axis/.style={code={
     \draw[step=1cm,gray,very thin] (-#1,-#1) grid (#1,#1);
     \draw[latex-latex] (-#1,0) -- (#1,0) node[right] {$x$-axis};
     \draw[latex-latex] (0,-#1) -- (0,#1) node[above] {$y$-axis};
    }}]
 \pic{axis=3.5};
 \foreach \X in {0.8,1.6,2.4}   
 {\pic[red,ultra thick,>=stealth]{span={vector 1={(\X,0)},vector 2={(0,\X)}}};}
\end{tikzpicture}
\end{document}

enter image description here

As you can see, this MWE also has a pic for the axis. It also avoids adding the linear combination in which both coefficients are 0.

Tags:

Tikz Pgf