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.


  • 5
    Have a look at chapter 2 of the TikZ manual (first tutorial).
    – Pier Paolo
    Commented Jan 13, 2015 at 11:23
  • 1
    A minimal xy axis is great and useful, but maybe next month you need something more specific. ;-) So think of that when you choose one of the answers. Commented Jan 13, 2015 at 15:20
  • 1
    I'm not sure what you meant @Arne, but thank you for the contribution. Cheers.
    – bru1987
    Commented Jan 13, 2015 at 16:29
  • 1
    Maybe, later you will add functions, or willing to controle the numbers on the x or y axe, or make it discontinue ... (for good practice of the forum just click the up arrow instead of typing thanks ;-) It is the way it goes here. Commented Jan 13, 2015 at 17:02
  • 1
    @PierPaolo Sir, the link to the tutorial has expired. Could you please update it? Commented Jan 7, 2021 at 14:00

12 Answers 12


with TiKZ:

\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$};


Minimal Axis

  • That's great Ignasi. I would do a slight modification, to do the dashed lines. Thank you!
    – bru1987
    Commented Jan 13, 2015 at 16:20
  • @bru1987 Glad to help you. Add dashed after help lines and the grid will be built with dashed lines.
    – Ignasi
    Commented Jan 13, 2015 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
    Commented Jan 13, 2015 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
    Commented Oct 23, 2020 at 12:24

Why pgfplots is left out?

               minor tick num=1,axis lines = middle,xlabel=$x$,ylabel=$y$,label style =
               {at={(ticklabel cs:1.1)}}]

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
    Commented Jan 13, 2015 at 19:47

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

\tkzGrid[sub,color=gray, subxstep=.5,subystep=.5]
\tkzAxeXY[very thick]

enter image description here

  • 1
    tkz-euclide I think is underappreciated. it is always my go to tikz-based package for quick plots.
    – hpesoj626
    Commented Sep 10, 2019 at 2:49

A PSTricks solution:




% 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}
  \psaxes[labels = none]{->}(0,0)(-\size,-\size)(\size.21,\size.21)[$x$,0][$y$,90]



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.

  • Hi Svend, that's a great looking image. How could I remove the number labels on it, keeping the spacing between them? Thank you!
    – bru1987
    Commented Jan 13, 2015 at 11:37
  • @bru1987 See updated answer. Commented Jan 13, 2015 at 11:59
  • That's a great solution @Svend, thank you for your contribution.
    – bru1987
    Commented Jan 13, 2015 at 16:12

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";


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;

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

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

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

% 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}}
\savestack\partA{\Longstack{\DL\\ \DL\\ \DL\\ \DL\\ \SL\\ \DL\\ \DL\\ \DL\\ \DL}}

enter image description here


No graphics question is complete without a picture mode solution:



enter image description here

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

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).

  \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;

    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;

    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;


    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));

Result as expected:

enter image description here


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.

    \begin{mfpfor}{i = ceiling(\xmin) step 1 until floor(\xmax)}
      \dashed\lines{(i, \ymin), (i, \ymax)}
    \begin{mfpfor}{j = ceiling(\ymin) step 1 until floor(\ymax)}
       \dashed\lines{(\xmin, j), (\xmax, j)}
    \tlabels{[tc](\xmax, 0){$x$} [cr](0, \ymax){$y$}}


enter image description here

\psaxes[labels=none,ticklinestyle=dashed,ticksize=-5 5,

enter image description here


An alternative MWE with PSTricks.

enter image description here

  \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]
  • Please do not use the minimal class for examples. It is not suitable.
    – cfr
    Commented Dec 27, 2016 at 21:03
  • I made the change by following your advice.
    – Sebastiano
    Commented Dec 27, 2016 at 21:10
\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);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .