I'd like to plot a vector field with a legend for the magnitude of a reference vector. I have read all the document of VectorPlot and found only PlotLegends is possible for this purpose. But I cannot come up with a solution. I also found a similar question here without the desired solution. So I post a minimal question here. Note that I do not want to visualize the magnitudes with colors, so please do not waste your time.

Question: How can I plot a vector field with a reference legend showing the reference magnitude? For example, in this figure, the arrow length of the legend should be related to that of the vector field to indicate a reference length.

VectorPlot[{y, -x}, {x, -3, 3}, {y, -3, 3}, PlotLegends -> {"Ref. vector"}]

enter image description here

  • $\begingroup$ What do you mean by reference length? if you run InputForm[VectorPlot[{y, -x}, {x, -3, 3}, {y, -3, 3}, PlotLegends -> {"Ref. vector"}]] you will see that PlotLegend is formed from an arrow of unit length Arrow[{{0, 0.5}, {1, 0.5}}] placed After. So, this unit arrow has reference length, in some sense at least. $\endgroup$ – Alx Sep 1 '19 at 13:37
  • $\begingroup$ @Alx reference length means a 'ruler' with a given length, which can be used to determine/estimate the magnitudes of other vectors in the field. Considering point (x=-3, y=3) gives vector (3,3) which has a length of about 4.243. So it is expected that the length of this arrow there has a length of 4.243 times of the reference length, i.e. the length of the legend. $\endgroup$ – user55777 Sep 1 '19 at 14:30
  • $\begingroup$ What about VectorColorFunction -> "Rainbow", PlotLegends -> Automatic? $\endgroup$ – Michael E2 Sep 1 '19 at 15:02
  • $\begingroup$ Maximal arrow length is Maximize[{Norm[{y, -x}], -3 <= x <= 3 && -3 <= y <= 3}, {x, y}] // First = 3 Sqrt[2]. Lengths of vectors are scaled to the diagonal of the plot range, in your case this is 6*Sqrt[2]. So, using VectorScale option and Legended: Legended[VectorPlot[{y, -x}, {x, -3, 3}, {y, -3, 3}, VectorScale -> {0.5, 0.1}], Graphics[{Arrowheads[0.3], Arrow[{{0, 0}, {1/(3 Sqrt[2]), 0}}]}]]. $\endgroup$ – Alx Sep 1 '19 at 16:09
  • $\begingroup$ @Alx so the absolute length of the legend is $1/(3 Sqrt[2])$ and the relative length in respect to the vector field is 1, right? $\endgroup$ – user55777 Sep 1 '19 at 16:15

In help page on VectorPlot (and VectorScale as well) we can read: "By default, the vector length is scaled by the norm of the vector field". And from the documentation on VectorScale it has the following settings: {unitlen, aratio, sfun}, where "unitlen is given as a fraction of the diagonal of the overall bounding box", i.e. diagonal of plot range.

For the example in question, maximal arrow length is given by Maximize[{Norm[{y, -x}], -3 <= x <= 3 && -3 <= y <= 3}, {x, y}] // First = 3 Sqrt[2], and the diagonal is 6 Sqrt[2]. Hence if we want to see arrows having their "native" length (not scaled as Mathematica does by default) we need to apply appropriate scaling:

VectorPlot[{y, -x}, {x, -3, 3}, {y, -3, 3}, VectorScale -> {0.5, 0.15},
PlotLegends -> {"Ref. vector"}]

enter image description here

If needed, number of arrows can be adjusted by VectorPoints option.

  • $\begingroup$ it is almost there! while it is still necessary to specify the arrow length of the legend to be unity (relative length), which is about $29\text{cm}/4.243=6.8$cm in absolute length. $\endgroup$ – user55777 Sep 2 '19 at 3:40

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.