Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to a show simple vector summation but I don't know how to draw a grid behind the vectors. I am pretty sure I can do it with Mathematica but how?

It should look like this:

Vector summation

share|improve this question
If you include your code for drawing the vectors, I'm sure we can help you out with drawing the grid. – R. M. Aug 26 '13 at 2:30

Mathematica has lots of Graphics primitives for you to work with, as well as directives such as Thick, Dashed, Red, etc. I'll use Arrow below.

You can specify the value of the GridLines option as a function. Using GridLines -> Range will give lines on a 1:1 grid starting from the extreme lower left of the graphic, as set with PlotRange or determined automatically.

If you want the grid lines to always snap to integer positions, even if the PlotRange is not bounded by integer values, you could use:

GridLines -> (Range[⌊#⌋, ⌈#2⌉]&)

Grid lines on a 2:2 grid can be had with Range[⌊#⌋, ⌈#2⌉, 2] &, etc.

I would construct a function such as this:

draw[a : {x_, y_}, b : {X_, Y_}, opts : OptionsPattern[]] :=
    {Thick, Blue, Dashed, Arrow[{a, {X, y}}], Arrow[{{X, y}, b}]},
    {Thick, Red, Arrow[{a, b}]}
   GridLines -> (Range[⌊#⌋, ⌈#2⌉]&),
   GridLinesStyle -> Directive[Thick, LightBlue]

And use it:

draw[{0, 0}, {7, 3}, PlotRangePadding -> 2]

enter image description here

Not included here are the labels that you can add with Text or Inset.

For some explanation of the options handling in the defined function see: Functions with Options

This answer is not intended to be a reference for the use of GridLines, only an illustration of one use: graph paper style. Nevertheless Kuba suggests I mention Method -> {"GridLinesInFront" -> True} and I think that's a good idea. This option draws the grid lines in front of all your graphics except those in the Epilog, if it exists.

draw[{0, 0}, {7, 3}, PlotRangePadding -> 2, Method -> {"GridLinesInFront" -> True}]

enter image description here

See my answer to Changing the background color of a framed plot for an example that uses both "GridLinesInFront" and Epilog.

share|improve this answer
Mr Wizard, do you have (a link to) an explanation of how your Gridlines function knows whose Slots it should use on evaluation? – wilbert van meerwijk Aug 26 '13 at 20:23
@wilbert the documentation for GridLines says: "func -- a function to be applied to Xmin, Xmax to get the grid line option" – Mr.Wizard Aug 26 '13 at 20:28
I see that, but am wrestling to understand the formal syntax. here func seems to grab the PlotRange of Graphics? – wilbert van meerwijk Aug 26 '13 at 20:45
@wilbert Yes, I believe that is how it works. I don't think it's spelled out quite that clearly but there are many examples in the documentation, starting with this one. – Mr.Wizard Aug 26 '13 at 21:06

GridLines works in Graphics

  {Thick, Darker[Red], Arrow[{{0, 0}, {1, 1}}]},
  {Dashed, Arrow[{{0, 0}, {1, 0}}]},
  {Dashed, Arrow[{{1, 0}, {1, 1}}]},
  Text[Style["R", Italic, Large], {.5, .5}, {0, -1}],
  Text[Style["\[Theta]", Large], {.2, .1}, {-1, 0}]
  }, GridLines -> Automatic, GridLinesStyle -> LightGray, 
 PlotRange -> {{-1, 2}, {-.5, 1.5}}, Frame -> True

Mathematica graphics

share|improve this answer
+1. I was working on an example just like this one. Only significant difference is I used PlotRangePadding, which is independent of where the arrows are positioned. :) – Pickett Aug 26 '13 at 2:49
@Anon Actually PlotRangePadding is a good idea; I hope you don't mind if I incorporate that into my answer. – Mr.Wizard Aug 26 '13 at 5:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.