Mathematica does provide beautiful visualization of vectors.

However, one puzzling situation I'm always facing is how to visualize the magnitude of vector fields. I'd like to have a plot as shown in the figure below with a box showing the magnitude of a reference vector.

How can I plot vector fields with legends showing the reference magnitude?

wind field

  • $\begingroup$ Can you include the code that you used to generate this figure? $\endgroup$
    – march
    Mar 19, 2016 at 21:06
  • $\begingroup$ @march this figure is generated with NCL, NCAR Command Language. Please refer to: ncl.ucar.edu/Applications/veceff.shtml $\endgroup$
    – sunt05
    Mar 19, 2016 at 21:11
  • $\begingroup$ VectorPlot with VectorScale and PlotLegends you can do this $\endgroup$
    – swish
    Mar 19, 2016 at 21:52
  • $\begingroup$ @swish one puzzle I find is how to precisely correlate the scale in PlotLegends to that in the graphics. If PlotLegends is placed within the graphics, it is possible but still not easy. Once the PlotLegends is placed outside the graphics, things become more challenging. $\endgroup$
    – sunt05
    Mar 19, 2016 at 22:12

1 Answer 1


If I understand your question correctly, your first idea for magnitude visualization is the comparison to a reference vector. The reason for your idea to not be realized in mathematica adhoc, is that there are other ways that are more vivid in showing magnitude of a vector field. Color is the keyword. There are two possibilities I can quickly think of, to implement the magnitude information via colors.

1. Vectors with colors

There is an option for the command VectorPlot called VectorColorFunction which defines a color palette representing the vector magnitude. This is by default scaled, which can be changed with the command VectorColorFunctionScaling. You can then use for example BarLegend to create a bar legend for your color palette. You will have to give the data range of your magnitudes. Below you can see an example of this.

With the code for this plot:

VectorPlot[{Exp[x], Exp[y]}, {x, -1, 1}, {y, -1, 1},
VectorColorFunction -> "Rainbow", 
PlotLegends -> BarLegend[{"Rainbow", {0.52026, 3.84423}}]]

Note, that I chose the range values in BarLegend manually as i checked the minimum and maximum values.

2. Coloring the background

There is another function for vector field illustration, called VectorDensityPlot. It works similar to VectorPlot. Just have a look at the options of this command and you will easily come to something like this:

  • $\begingroup$ I just thought of another way to illustrate the magnitude. You could use values overlapping with the arrows, but this is by far not as vivid as my suggestions above. If you are interested I may provide some input for that as well. $\endgroup$ Nov 8, 2016 at 14:45
  • $\begingroup$ Thanks for the answer! And I'm sorry about the delay of replying your answer. Using colours upon vectors is definitely a feasible way to visualise the magnitudes. However, my initial intent was to reproduce the figure shown in the question as colours were reserved for other purposes (e.g., colours and contours will be denote background temperature and humidity, respectively). As such, I'm afraid I cannot accept this answer and would rather wait for other solutions. $\endgroup$
    – sunt05
    Nov 16, 2016 at 16:58
  • $\begingroup$ @sunt05 Well ok, if you just want to reproduce your picture, I will look into that, if I have some time. So I assume, accurate information about the magnitude of the arrows is not that important? With a well formatted 3D-Plot you might also get what you want as you could put both informations (magnitude and e.g. temperature) into one plot. But that's a question of readability of the plot. let me know what you think $\endgroup$ Nov 17, 2016 at 10:12
  • $\begingroup$ Yes: there would be readability issues if such plots go into the 3D regime as I intended to use them for publications. And one strategy I'm thinking about is the Inset approach that can combine several plot-like elements into one canvas. Thanks for your help. $\endgroup$
    – sunt05
    Nov 17, 2016 at 11:20

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.