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$
6
  • $\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, 2019 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$ Jan 16, 2019 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, 2019 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$ Jan 16, 2019 at 18:15
  • 3
    $\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, 2019 at 20:56

2 Answers 2

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$
7
  • $\begingroup$ plotVectors[pts : {{_?NumberQ, _?NumberQ} __}] I've never seen that construction before. Came here to learn something. $\endgroup$
    – BBirdsell
    Jan 17, 2019 at 3:53
  • $\begingroup$ @m_goldberg wow, that was neat. thanks a lot! $\endgroup$ Jan 17, 2019 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, 2019 at 23:38
  • $\begingroup$ @m_goldberg accepted. Thanks again. $\endgroup$ Jan 18, 2019 at 9:37
  • 1
    $\begingroup$ I'm pretty puzzled by the __ on the end. Did you actually mean plotVectors[pts : {{_?NumberQ, _?NumberQ} ..}] := Graphics[Arrow[{{0, 0}, #} & /@ pts], Axes -> True] ? $\endgroup$
    – kirma
    Jan 7, 2023 at 18:43
0
$\begingroup$

I'd like to show this code, which is essentially the same as from m_goldberg, but it uses Standard colors. And adds label, the reason I did this, is because I needed this on my own plots and wanted to share.

plotVector[vectors : {{_?NumberQ, _?NumberQ} ..}, 
   OptionsPattern[{"Label" -> False}]] := 
  Block[{arrows, labels}, 
   arrows = 
    MapIndexed[
     Style[Arrow[{{0, 0}, #1}], ColorData[97][#2[[1]]], Thick] &, 
     vectors];
   labels = 
    MapIndexed[
     Style[Text[#1, #1 + {0.2, 0.2}, {1, 1}], 
       ColorData[97][#2[[1]]]] &, vectors];
   Graphics[
    Join[{Arrowheads[0.05]}, arrows, 
     If[OptionValue["Label"], labels, {}]], Axes -> True]];

This function takes a list of 2D vectors as input and plots them as arrows on a 2D plot, with the origin {0, 0} as the starting point and the vector as the end point. The arrows are styled using the Style function, with the color generated using the ColorData function and the thickness specified using the Thick option. The x-axis and y-axis are displayed on the plot using the Axes option, and the size of the arrowheads is specified using the Arrowheads option.

You use it like this: plotVector[{{1, 1}, {2, 3}, {0, 1}, {1, 0}}, Label -> True], in order to get:

Mathematica screenshot plotting the result of plotVector[{{1, 1}, {2, 3}, {0, 1}, {1, 0}}, Label -> True]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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