# Plotting Vectors in Mathematica

I'm trying to plot 2D vectors in Mathematica. Built in functions don't really work for me because I want to plot vectors of matrices from the origin to the their coordinates with an arrow on their tips. I made a function

plotMatrixVectors[mat_List] :=
Graphics[Table[Arrow[{origin, i}], {i, mat}], Axes -> True],


and it worked just fine, until it suddenly stopped working and instead of a graph started reporting this to me:

{{plotMatrixVectors, plotMatrixVectors}, {plotMatrixVectors,
plotMatrixVectors}}


Note: these numbers in square bracket have nothing to the with coordinates in the lower example, this message came up when I tried to plot different vectors, but you get the point.

I rewrote the function because I thought I unintentionally messed it up somehow, but it still printed the same thing. However, when I copypasted the body of the function and just plugged in a 2x2 matrix, say this one

plot2D1 = {{1, 5}, {-6, 4}};

Graphics[Table[Arrow[{origin, i}], {i, plot2D1}], Axes -> True]


it gives me this, which is good. I have 2 questions here. 1) Why did my function stopped working but the same thing works when it's typed outside of the function? I restarted Mathematica and it still didn't work. 2) Is there any way to strecth those axes so they go beyond the limits of matrix vectors coordinates?

Thanks a lot!

• Can you provide an example of the exact command you're typing when it gives the bad output? Without seeing an example, I'm going to guess that you're trying to give it a list with the wrong dimensions and Table isn't behaving the way you expect. – N.J.Evans Jan 16 '19 at 17:51
• Sure, here it is: origin = {0, 0}; matrix = {{1, 2}, {-4, 5}}; plotMatrixVectors[mat_List] := Graphics[Table[Arrow[{origin, i}], {i, mat}], Axes -> True] plotMatrixVectors[matrix] Out= {{plotMatrixVectors, plotMatrixVectors}, {plotMatrixVectors[-4], plotMatrixVectors}} Sorry for this mess, don't know how to properly copy it. Hope you'll spot something, 'cause I've tried again and still I see nothing wrong – Nikola Kojadinović Jan 16 '19 at 17:56
• @NikolaKojadinović It is unlikely that that is a full report. The function works fine. The result you're getting is equivalent to plotMatrixVectors /@ Catenate@matrix. – Alan Jan 16 '19 at 18:06
• @Alan it is a full report. However, it works fine now after I restarted mathematica 5 times. Do you think it could've been a bug? – Nikola Kojadinović Jan 16 '19 at 18:15
• You could have had a definition you forgot to clear. Otherwise it's very hard to say what's going on if something like that goes away after a restart. If it shows up again, check back. See the docs on the option PlotRange' for the axes. – N.J.Evans Jan 16 '19 at 20:56

I don't know what went wrong with your code, but you might try this. It is simpler and more robust than your formulation.

plotVectors[pts : {{_?NumberQ, _?NumberQ} __}] :=
Graphics[Arrow[{{0, 0}, #} & /@ pts], Axes -> True]


Tests

plotVectors[{{1, 5}, {-6, 4}} SeedRandom;
With[{n = 15}, plotVectors[RandomReal[{-10, 10}, {n, 2}]]] • plotVectors[pts : {{_?NumberQ, _?NumberQ} __}] ` I've never seen that construction before. Came here to learn something. – BBirdsell Jan 17 '19 at 3:53
• @m_goldberg wow, that was neat. thanks a lot! – Nikola Kojadinović Jan 17 '19 at 20:33
• @NikolaKojadinović. I'm glad you find my answer useful. Please consider accepting it. You can do that by clicking on the check mark that appears on the left of the answer below the down arrow. – m_goldberg Jan 17 '19 at 23:38
• @m_goldberg accepted. Thanks again. – Nikola Kojadinović Jan 18 '19 at 9:37