2
$\begingroup$

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[1], plotMatrixVectors[2]}, {plotMatrixVectors[3], 
  plotMatrixVectors[1]}} 

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. enter image description here

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!

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – N.J.Evans Jan 16 at 17:51
  • $\begingroup$ 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[232]= {{plotMatrixVectors[1], plotMatrixVectors[2]}, {plotMatrixVectors[-4], plotMatrixVectors[5]}} 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 $\endgroup$ – Nikola Kojadinović Jan 16 at 17:56
  • $\begingroup$ @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. $\endgroup$ – Alan Jan 16 at 18:06
  • $\begingroup$ @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? $\endgroup$ – Nikola Kojadinović Jan 16 at 18:15
  • 2
    $\begingroup$ 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. $\endgroup$ – N.J.Evans Jan 16 at 20:56
6
$\begingroup$

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}}

test1

SeedRandom[26];
With[{n = 15}, plotVectors[RandomReal[{-10, 10}, {n, 2}]]]

test2

$\endgroup$
  • $\begingroup$ plotVectors[pts : {{_?NumberQ, _?NumberQ} __}] I've never seen that construction before. Came here to learn something. $\endgroup$ – BBirdsell Jan 17 at 3:53
  • $\begingroup$ @m_goldberg wow, that was neat. thanks a lot! $\endgroup$ – Nikola Kojadinović Jan 17 at 20:33
  • $\begingroup$ @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. $\endgroup$ – m_goldberg Jan 17 at 23:38
  • $\begingroup$ @m_goldberg accepted. Thanks again. $\endgroup$ – Nikola Kojadinović Jan 18 at 9:37

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.