57

can anybody help me to draw this minimal example of an xy axis? I couldn't find anything similar on the forum.

The axis should be black but the grid behind it a shade o gray, dashed. It would be great if the axis were a little bit thicker than the grid also.

Axis

bru1987
  • 903

12 Answers12

66

with TiKZ:

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!30, dashed] (-4.9,-4.9) grid (4.9,4.9);
\draw[->,ultra thick] (-5,0)--(5,0) node[right]{$x$};
\draw[->,ultra thick] (0,-5)--(0,5) node[above]{$y$};

\end{tikzpicture}
\end{document}

Minimal Axis

bru1987
  • 903
Ignasi
  • 136,588
  • That's great Ignasi. I would do a slight modification, to do the dashed lines. Thank you! – bru1987 Jan 13 '15 at 16:20
  • @bru1987 Glad to help you. Add dashed after help lines and the grid will be built with dashed lines. – Ignasi Jan 13 '15 at 16:26
  • Thank you @Ignasi, I just did that, along with making it a lighter black. I hope you like the modification. Thank you. – bru1987 Jan 13 '15 at 16:28
  • You can use the option of Triangle-Triangle within the \draw command to have better arrows. You will need to load the arrow package in the usetikzlibrary. – Nick B Oct 23 '20 at 12:24
41

Why pgfplots is left out?

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[grid=both,ymin=-5,ymax=5,xmax=5,xmin=-5,xticklabel=\empty,yticklabel=\empty,
               minor tick num=1,axis lines = middle,xlabel=$x$,ylabel=$y$,label style =
               {at={(ticklabel cs:1.1)}}]
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    +1 Honestly, I didn't think of this possibility. But now that we are rolling, you could mention of adding axis equal=true to have the same scale in both axes. – Pier Paolo Jan 13 '15 at 19:47
17

A PSTricks solution:

\documentclass{article}

\usepackage{pst-plot}

\begin{document}

% parameter
\def\size{5} % natural number

\begin{pspicture}(-\size,-\size)(\size.57,\size.62)% found manually
  \multido{\iA = -\size+1}{\numexpr2*\size+1}{%
    \multido{\iB = -\size+1}{\numexpr2*\size+1}{%
    \psset{linewidth = 0.5\pslinewidth, linestyle = dashed, linecolor = gray!50}
      \psline(-\size,\iA)(\size,\iA)
      \psline(\iB,-\size)(\iB,\size)}}
  \psaxes[labels = none]{->}(0,0)(-\size,-\size)(\size.21,\size.21)[$x$,0][$y$,90]
\end{pspicture}

\end{document}

output

All you have to do is choose the value of \size and the drawing will be adjusted accordingly.

I've drawn the grid manually since pstricks-add's function for this makes it look wierd, I think.

17

And here is a fairly generic Metapost approach to making a grid. Follow the link for an introduction to Metapost and how to use this code in a LaTeX document.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

u = 1cm;
ymax = xmax = 4.75;
xmin = ymin = -4.75;

path xx, yy;
xx = ((xmin,0) -- (xmax,0)) scaled u;
yy = ((0,ymin) -- (0,ymax)) scaled u;

drawoptions(dashed evenly scaled .5 withcolor .7 white);
for i = ceiling ymin upto floor ymax: draw xx shifted (0,i*u); endfor
for i = ceiling xmin upto floor xmax: draw yy shifted (i*u,0); endfor

drawoptions(withpen pencircle scaled .7);
xx := xx scaled 1.05;
yy := yy scaled 1.05;
drawarrow xx;
drawarrow yy;
drawoptions();

label.rt (btex $x$ etex, point infinity of xx);
label.top(btex $y$ etex, point infinity of yy);

endfig;
end.
Thruston
  • 42,268
  • Having never worked with Metapost myself, I may ask a (maybe silly) question: how do I incorporate this code in my LaTeX document? – Jan Dec 27 '16 at 19:24
  • Not silly at all - if you follow the link at the top of my answer, that should get you going. – Thruston Dec 27 '16 at 19:28
17

Just a simple way, easy to adapt, (but the lines or not dashed).

\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
\tkzInit[xmin=-5,xmax=5,ymin=-5,ymax=5]
\tkzGrid[sub,color=gray, subxstep=.5,subystep=.5]
\tkzAxeXY[very thick]
\tkzGrid
\end{tikzpicture}
\end{document}

enter image description here

16

A stack was too much to resist, so onto the bandwagon I jump.

\documentclass{article}  
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor,graphicx,amssymb}
\setstackgap{L}{1cm}
\def\stacktype{L}
% DASHED LINE OF SPECIFIED LENGTH
% From morsburg at http://tex.stackexchange.com/questions/12537/
% how-can-i-make-a-horizontal-dashed-line/12553#12553
\def\solidfill{\cleaders\hbox to .1cm{\rule{.1cm}{1pt}}\hfill}
\def\dashfill{\cleaders\hbox to .2cm{\rule{.05cm}{.4pt}}\hfill}
\newcommand\dashline[1]{\hbox to #1{\dashfill\hfil}}
\newcommand\solidline[1]{\hbox to #1{\solidfill\hfil}}
\newcommand\DL{\textcolor{black!30}{\dashline{8.6cm}}}
\newcommand\SL{\textcolor{black}{\solidline{8.8cm}}\makebox[.2cm][r]{\arrowhead}}
\def\arrowhead{\raisebox{-2.6pt}{$\blacktriangleright$}}
\begin{document}
\savestack\partA{\Longstack{\DL\\ \DL\\ \DL\\ \DL\\ \SL\\ \DL\\ \DL\\ \DL\\ \DL}}
\stackinset{c}{10pt}{t}{3pt}{$y$}{%
  \stackinset{r}{3pt}{c}{-10pt}{$x$}{%
    \stackon[-.5cm]{\partA}{\rotatebox{90}{\partA}}%
}}
\end{document}

enter image description here

15

No graphics question is complete without a picture mode solution:

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\unitlength=1cm

\begin{document}
\begin{picture}(10.5,10.5)(-5,-5)
  {\color{gray}
  \thinlines
  \multiput(-5,-4)(0,1){9}{\line(1,0){10}}
  \multiput(-4,-5)(1,0){9}{\line(0,1){10}}
  }
  \thicklines
  \put(-5,0){\vector(1,0){10.2}}
  \put(0,-5){\vector(0,1){10.2}}
  \put(5.3,0){\makebox(1,0)[l]{$x$}}
  \put(0,5.3){\makebox(0,1)[b]{$y$}}
\end{picture}
\end{document}

enter image description here

Paul Gessler
  • 29,607
  • What a pitty, that I only can add +1 to your counter. You are so right :-) This would also be my answer. – Jan Dec 27 '16 at 19:25
13

My own try with MetaPost, which is integrated into a LuaLaTeX program. I've picked up some macros I had already created for my own use (and modified some of them slightly).

\documentclass[12pt]{scrartcl}
\usepackage{unicode-math}
\usepackage{luamplib}
  \everymplib{verbatimtex \leavevmode etex;

    def hgrid(expr ymin, ymax, ystep, u, v)(text mycolor) =
      for j = ceiling(ymin) step ystep until floor(ymax):
        draw ((xmin, j) -- (xmax, j)) xscaled u yscaled v withcolor mycolor;
      endfor;
    enddef;

    def vgrid(expr xmin, xmax, xstep, u, v)(text mycolor) =
      for i = ceiling(xmin) step xstep until floor(xmax):
        draw ((i, ymin) -- (i, ymax)) xscaled u yscaled v withcolor mycolor;
      endfor;
    enddef;

    vardef xaxis(expr xmin, xmax, u) = ((xmin, 0) -- (xmax, 0)) scaled u enddef;
    vardef yaxis(expr ymin, ymax, v) = ((0, ymin) -- (0, ymax)) scaled v enddef;

    beginfig(1);}
  \everyendmplib{endfig;}

\begin{document}
  \begin{mplibcode}
    u := 1cm; v := 1.25cm;
    xmin = -4.9; xmax = 4.9; ymin = -4.5; ymax = 4.5;
    vgrid(xmin, xmax, 1, u, v, 0.8white dashed evenly); 
    hgrid(ymin, ymax, 1, u, v, 0.8white dashed evenly);
    pickup pencircle scaled 1bp;
    drawarrow xaxis(xmin, xmax, u); drawarrow yaxis(ymin, ymax, v);
    label.bot(btex $x$ etex, (xmax*u, 0)); label.lft(btex $y$ etex, (0, ymax*v));
 \end{mplibcode}
\end{document}

Result as expected:

enter image description here

Franck Pastor
  • 18,756
12

This time with the mfpic package, which is a (La)TeX interface to MetaPost or Metafont (here used MetaPost). Note that this program is shorter than my correponding MetaPost coding, since mfpic already has its own drawing macros.

In exchange it requires several compilations, à la bibTeX: first compile your LaTeX file, say grid.tex, with LaTeX, then the created grid.mp file with MetaPost, and then again grid.tex with LaTeX. As a regular mfpic user I use a script which automatizes this.

\documentclass[12pt]{scrartcl}
\usepackage[metapost]{mfpic}
  \setlength{\mfpicunit}{1cm}
\opengraphsfile{\jobname}
\begin{document}
  \begin{mfpic}[1]{-4.9}{4.9}{-4.5}{4.5}
    \drawcolor[gray]{0.8}
    \begin{mfpfor}{i = ceiling(\xmin) step 1 until floor(\xmax)}
      \dashed\lines{(i, \ymin), (i, \ymax)}
    \end{mfpfor}
    \begin{mfpfor}{j = ceiling(\ymin) step 1 until floor(\ymax)}
       \dashed\lines{(\xmin, j), (\xmax, j)}
    \end{mfpfor}
    \drawcolor{black}
    \penwd{1bp}
    \doaxes{xy}
    \tlpointsep{3bp}
    \tlabels{[tc](\xmax, 0){$x$} [cr](0, \ymax){$y$}}
  \end{mfpic}
\closegraphsfile
\end{document}

Result:

enter image description here

Franck Pastor
  • 18,756
5
\documentclass{article}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}(-5.4,-5.4)(5.4,5.4)
\psaxes[labels=none,ticklinestyle=dashed,ticksize=-5 5,
        tickcolor=black!20](0,0)(-5,-5)(5,5)[$x$,0][$y$,90]
\psaxes[labels=none]{->}(0,0)(-5,-5)(5,5)
\end{pspicture}
\end{document}

enter image description here

1

An alternative MWE with PSTricks.

enter image description here

\documentclass{article}
\usepackage{pstricks-add}
 \begin{document}
   \begin{pspicture}(-5,-5)(5,5)
  \psgrid[griddots=10,gridlabels=0pt, subgriddiv=0, gridcolor=darkgray]
\psaxes[linewidth=1.2pt, labels=all]{->}(0,0)(-5,-5)(5,5)[$x$,-90][$y$,-180]
   \end{pspicture}
\end{document}
Sebastiano
  • 54,118
0
\begin{figure[ht]
\centering
\begin{tikzpicture]\draw[step=0.5cm,opacity=0.5] (-5,-5) grid (5,5);
\draw[Triangle-Triangle,ultra thick] (-5,0)--(5,0) node[below right]{$x$};
\draw[Triangle-Triangle,ultra thick] (0,5) node[above left]{$y$}--(0,-5);
\end{tikzpicture}
\end{figure}
Werner
  • 603,163
Nick B
  • 831