12

How can I create a simple coordinate system with a linear line in LaTeX using the tikz package? It should look something like this. With the iterating numbers on each axis and all.

enter image description here

  • 6
    Did you read the first tutorial in pgfmanual? – Ignasi Jan 21 '14 at 11:44
15

Use the tkz-euclide package for simple output.

\documentclass[tikz]{standalone}

\usepackage{tkz-euclide}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
   \tkzInit[xmax=6,ymax=6,xmin=-6,ymin=-6]
   \tkzGrid
   \tkzAxeXY
   \draw[ thick,latex-latex] (-1,4) -- (4,-6) node[anchor=south west] {$a$}; % two points for drawing 2x+y=2
  \tkzText[above](0,6.75){Desired Output}
  \end{tikzpicture}
\end{figure}
\end{document}

Output:

output

  • Works great, do you know if it is possible to add title/header above the coordinate system? – Oskar Persson Jan 21 '14 at 13:19
  • 1
    @OskarPersson, use \tkzText [above](0,6.5){$E=mc^2$} for insert text whenever you want. – ferahfeza Jan 21 '14 at 14:42
7

What, no pgfplots example?

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  axis line style={Stealth-Stealth,very thick},
  xmin=-5.5,xmax=5.5,ymin=-5.5,ymax=5.5,
  xtick distance=1,
  ytick distance=1,
  xlabel=$x$,
  ylabel=$y$,
  title={Wonderful plot},
  grid=major,
  grid style={thin,densely dotted,black!20}]
\addplot [Latex-Latex,domain=-5:3,samples=2] {x*2/3} node[right]{$a$};
\end{axis}
\end{tikzpicture}
\end{document}
6

This is one possible solution. Here both Euclidean and polar coordinates are presented. Furthermore, Give two points, a line is drawn by extending the end points

extended line/.style={shorten >=-#1,shorten <=-#1}

A macro drawing a line, given two points, is added.

\newcommand{\drawaline}[4]{
\draw [extended line=1cm,stealth-stealth] (#1,#2)--(#3,#4);
}

enter image description here

Code

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}
\newcommand{\drawaline}[4]{
\draw [extended line=1cm,stealth-stealth] (#1,#2)--(#3,#4);
}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.5,extended line/.style={shorten >=-#1,shorten <=-#1},]
\draw [help lines] (-3,-3) grid (3,3);
% Euclidean
\draw [->](0,-2.2)--(0,2.2) node[right]{$y$};
\draw [->](-2.2,0)--(2.2,0) node[right]{$x$};
% polar coordinate
\draw (0,0)--(2,2)node[anchor=south west]{$x+jy=r\angle \theta$};
\draw [thin,dashed] (2,2)--(2,0);
\draw (0.8,1)node[anchor=west,rotate=45]{$r=\sqrt{x^2+y^2}$};
\draw [fill=green](0,0) -- (0.75,0) arc (0:45:0.75cm);
\draw (1,0.3) node[rotate=10]{$\theta=\tan^{-1} \frac{y}{x}$};
\fill [red](2,2) circle(2pt);

% draw ticks and its labels
\foreach \x/\xtext in {-2/-2, -1.5/-\frac{3}{2}, -1/-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1/1, 1.5/\frac{3}{2}, 2/2}
{\draw (\x cm,1pt ) -- (\x cm,-1pt ) node[anchor=north] {$\xtext$};}
\foreach \y/\ytext in {-2/-2, -1.5/-\frac{3}{2}, -1/-1, -0.5/-\frac{1}{2},0.5/\frac{1}{2}, 1/1, 1.5/\frac{3}{2}, 2/2}
{\draw (1pt,\y cm) -- (-1pt ,\y cm) node[anchor=east] {$\ytext$};}

% draw lines
\draw [extended line=1cm,stealth-stealth] (-2,-1)--(1,2) node[above]{a};  % method 1
\drawaline{0}{-2}{-2}{0}                                                  % method 2
\end{tikzpicture}
\end{center}

\end{document}
  • Do you have simple example where it can just render the line for 2x +y = 2 – Oskar Persson Jan 21 '14 at 12:05
  • Yes, just draw two points and extend it. I will update my post – Jesse Jan 21 '14 at 12:20
6

A PSTricks solution:

\documentclass{article}

\usepackage{pst-plot}

\begin{document}

\begin{figure}[htbp]
\centering
 \psset{unit = 0.7}
  \begin{pspicture}(-6,-6)(6.5,6.5)
    \psaxes{<->}(0,0)(-6,-6)(6,6)[$x$,0][$y$,90]
    \psline{<->}(-6,-1)(3,2)
    \uput[0](3,2){a}
  \end{pspicture}
 \caption{Plot of a line.}
 \label{figure:1}
\end{figure}

\noindent This is Figure~\ref{figure:1}.

\end{document}

output

5

Just another fun with PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}

\usepackage{pst-plot}
\psset{algebraic,arrows=<->}
\def\f{x/3+1}

\begin{document}
\begin{pspicture}(-6,-3)(6.5,4.5)
    \psaxes[linewidth=.4pt,linecolor=gray](0,0)(-6,-3)(6,4)[$x$,0][$y$,90]
    \psplot[linecolor=blue]{-5}{5}{\f}
    \rput(*5.2 {\f}){$a$}
\end{pspicture}
\end{document}

enter image description here

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.