pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
187 stars 33 forks source link

Easier Mathemetica-like VectorPlots #469

Open MithicSpirit opened 4 months ago

MithicSpirit commented 4 months ago

While quiver plots can be made to emulate VectorPlots, it is quite annoying. In particular, the main issue is representing vector magnitude with a color rather than as the length of the arrow. Currently, it is possible to fix this by dividing each coordinate by the magnitude of the vector, setting the point meta to the magnitude, and telling it to color the arrows. If the scales of the axes are not the same, this must also be adjusted for. Also, it may be necessary to mess around with the arrow scale to prevent overlap between the arrows. This usecase seems to be entirely undocumented in the 1.18.1 manual, which further adds to its difficulty.

It would be much more convenient if there was an option that automatically normalized all the vectors. This should account for differing scales, and, if possible, the distance between neighboring arrows (I've found that around 80% of the distance between an arrow and its nearest neighbor looks best). Another option (or maybe the same) that set the point meta to the (pre-normalized) magnitude would also be helpful.

As an example of the problem this would fix, here is a before an after with some sample option names:

% before
\addplot3 [
  samples = 12,
  point meta = {sqrt((0.5*y)^2 + (-6*x)^2)},
  quiver = {
    u = {0.5*y / sqrt((0.5*y)^2 + (-6*x)^2)},
    v = {-6*x / sqrt((0.5*y)^2 + (-6*x)^2)},
    colored,
    scale arrows = 0.03,
    },
  -latex,
] {0};

% after
\addplot3 [
  samples = 12,
  quiver = {
    u = {0.5*y},
    v = {-6*x},
    normalize,
    point meta magnitude,
    colored,
    },
  -latex,
] {0};

This, of course, does not show how long I spent trying to figure out how to do this, and then messing around with scale arrows, and then doing so again when I decided to change my domains and samples. While there is a trick with setting the z variable to the magnitude of the vector and then using that variable in point meta, u, and v, this does not work in all situations.