# ListVectorDensityPlot plots outside the data in a very wrong way

I am plotting a vector field data set mydata where the background color indicates the vector orientation and its opacity for the vector magnitude. I use ColorFunction -> Function[{x, y, vx, vy, n}, Hue[ArcTan[vx, vy]/(2 π), n]] to get the rainbow-like color, in which n signifies the norm slot.

I conceived of two methods to plot it

• plot the original mydata with the option ColorFunctionScaling -> {False, False, False, False, True} that presumably normalizes the norm to be within $$[0,1]$$
• normalize vectors with the maximal norm in mydata to get mydatanormalized and then just plot it with the option ColorFunctionScaling -> False
mydata = ArrayReshape[#, {21, 21, 2}] &@ImportString[#, "List", "LineSeparators" -> " "] &@"-0.1053 0.1218 -0.1049 0.1505 -0.1375 0.1595 -0.1487 0.1927 -0.1451 0.2315 -0.1215 0.2768 -0.04844 0.3219 0.03322 0.3269 0.07211 0.2912 0.08432 0.2518 0.1131 0.2321 0.1724 0.2324 0.2272 0.2463 0.2196 0.2704 0.1544 0.2909 0.1152 0.2789 0.1279 0.2363 0.1452 0.1922 0.1434 0.1616 0.109 0.1527 0.09839 0.1137 -0.1213 0.136 -0.1614 0.1428 -0.1732 0.1841 -0.1728 0.2311 -0.1156 0.2995 -0.04907 0.2926 -0.1552 0.1407 -0.3534 -0.01118 -0.4592 -0.07593 -0.4765 -0.08614 -0.4852 -0.06397 -0.4895 -0.007719 -0.3949 0.06222 -0.1099 0.1005 0.2227 0.1192 0.2996 0.1942 0.1889 0.2662 0.1568 0.2377 0.1702 0.1815 0.1622 0.1482 0.122 0.1418 -0.1739 0.1208 -0.1922 0.1645 -0.1975 0.2172 -0.1333 0.291 -0.1719 0.1549 -0.4066 -0.1208 -0.3151 -0.1887 0.0631 -0.2345 0.2625 -0.3867 0.235 -0.5172 0.1542 -0.5666 0.08391 -0.545 -0.06646 -0.4208 -0.3682 -0.1554 -0.4933 0.08776 -0.06011 0.07589 0.3353 0.07367 0.2538 0.2236 0.1863 0.2226 0.192 0.159 0.1715 0.1322 -0.2041 0.1351 -0.22 0.1858 -0.173 0.2642 -0.2533 0.04761 -0.3845 -0.2177 0.1426 -0.1476 0.4323 -0.4011 0.01581 -0.6387 -0.3758 -0.4889 -0.4999 -0.2379 -0.5455 -0.1084 -0.6077 -0.1333 -0.5658 -0.3121 -0.2859 -0.5633 -0.1657 -0.4986 -0.4871 0.007491 -0.2228 0.1038 0.3369 -0.002831 0.2738 0.1983 0.2105 0.1894 0.2056 0.1288 -0.2311 0.1454 -0.218 0.2266 -0.2534 0.05052 -0.3389 -0.2386 0.3436 -0.07197 0.3474 -0.5235 -0.2275 -0.4495 -0.113 0.2096 0.2023 0.532 0.3474 0.5644 0.4112 0.5295 0.405 0.488 0.1526 0.4604 -0.4097 0.3655 -0.6207 -0.1181 -0.2755 -0.5577 -0.4798 0.004167 -0.1684 0.08243 0.3685 -0.02977 0.2602 0.1968 0.2284 0.1393 -0.253 0.1604 -0.2334 0.1419 -0.3543 -0.2607 0.271 0.01476 0.4151 -0.4706 -0.1293 -0.2393 0.129 0.5738 0.1148 0.5778 -0.09508 0.505 -0.116 0.5551 -0.004217 0.5867 0.1707 0.5495 0.423 0.4366 0.5743 0.3617 0.0175 0.5135 -0.618 0.1333 -0.3555 -0.5048 -0.5089 0.1697 0.08054 -0.06534 0.358 0.048 0.2506 0.1613 -0.2582 0.1718 -0.308 -0.1391 -0.05642 -0.07989 0.5825 -0.1667 0.008296 -0.3498 0.08211 0.6251 -0.1607 0.4409 -0.4303 0.4275 -0.3143 0.5939 -0.1327 0.6513 0.03406 0.6559 0.1883 0.6363 0.2908 0.5872 0.4234 0.4293 0.6435 0.2429 0.03715 0.5649 -0.579 -0.01969 -0.4309 -0.2641 -0.3364 0.195 0.3361 -0.16 0.2943 0.1476 -0.2673 0.1044 -0.3049 -0.2994 0.3626 0.1816 0.3727 -0.472 0.001924 0.4332 -0.1696 0.4304 -0.5591 0.2757 -0.4592 0.4918 -0.3519 0.5183 -0.2659 0.5616 -0.09348 0.6183 0.1314 0.5989 0.361 0.513 0.4824 0.4538 0.5533 0.2773 0.6248 0.2195 -0.2896 0.5875 -0.4982 -0.3974 -0.4863 0.2603 0.1407 -0.1899 0.3439 0.05582 -0.2786 -0.0324 -0.1941 -0.2044 0.5641 0.1394 0.1785 -0.2592 -0.05808 0.6511 -0.5027 0.08997 -0.6128 0.3006 -0.5299 0.3401 -0.5251 0.3802 -0.3475 0.5655 -0.01929 0.6477 0.2287 0.6177 0.3663 0.5083 0.5362 0.3325 0.5904 0.272 0.6829 0.03091 0.2214 0.5812 -0.5499 -0.08476 -0.4961 0.1056 -0.06765 -0.06919 0.359 -0.07163 -0.2761 -0.1554 -0.08473 -0.03167 0.6094 0.01855 0.07798 0.1012 -0.1947 0.4449 -0.6302 -0.03477 -0.6369 0.1938 -0.6219 0.13 -0.6111 0.2534 -0.303 0.4264 0.09962 0.3719 0.4239 0.3184 0.5455 0.3559 0.5647 0.234 0.648 0.131 0.6571 -0.03595 0.4996 0.3753 -0.4662 0.2026 -0.51 -0.00885 -0.1832 0.04382 0.3393 -0.171 -0.2615 -0.2213 -0.04926 0.07117 0.6149 0.00897 0.024 0.2857 -0.2641 0.2578 -0.6493 -0.1399 -0.6557 0.02893 -0.6399 -0.08074 -0.6387 -0.0007441 -0.3608 0.09465 0.007346 -0.0009028 0.3769 -0.08724 0.6345 0.03261 0.6116 0.06239 0.6586 -0.03337 0.6447 -0.1409 0.5834 0.2681 -0.3904 0.2822 -0.5222 0.009543 -0.2105 0.06824 0.3155 -0.2184 -0.2472 -0.2323 -0.0873 0.05752 0.5964 0.1293 0.04292 0.2784 -0.2628 0.2395 -0.5928 -0.2914 -0.6531 -0.1266 -0.5859 -0.2606 -0.5652 -0.3251 -0.4119 -0.3033 -0.08226 -0.3658 0.3187 -0.4094 0.6001 -0.2264 0.5953 -0.1647 0.6324 -0.1912 0.6217 -0.2794 0.5702 0.3252 -0.4316 0.1705 -0.4969 0.1579 -0.1811 -0.02403 0.3108 -0.2095 -0.245 -0.1963 -0.1483 -0.08369 0.4783 0.3497 0.2037 0.1327 -0.2708 0.4133 -0.4239 -0.4292 -0.6278 -0.2823 -0.5278 -0.3669 -0.4059 -0.5047 -0.2513 -0.5934 0.01406 -0.6254 0.3336 -0.5451 0.4879 -0.3919 0.5248 -0.3732 0.5617 -0.3413 0.6158 -0.3397 0.4355 0.488 -0.5753 -0.0458 -0.4104 0.3839 -0.1143 -0.2249 0.3265 -0.1465 -0.2622 -0.133 -0.1644 -0.2877 0.1814 0.5119 0.4972 0.03027 -0.2991 0.5835 -0.2053 -0.2953 -0.4827 -0.4978 -0.5046 -0.4433 -0.3522 -0.5418 -0.1633 -0.6184 0.04957 -0.6333 0.2286 -0.5891 0.3474 -0.5458 0.438 -0.4952 0.4755 -0.5012 0.6741 -0.06038 0.01007 0.4247 -0.6218 -0.0444 -0.306 0.4313 -0.0005853 -0.4014 0.3409 -0.07037 -0.2828 -0.0945 -0.1467 -0.3714 -0.1334 0.2989 0.537 0.2901 0.00376 0.3698 -0.293 0.2969 -0.1096 -0.5334 -0.3542 -0.6048 -0.3511 -0.5872 -0.1966 -0.6331 -0.02812 -0.6585 0.125 -0.6499 0.2403 -0.6243 0.3452 -0.608 0.6147 -0.312 0.4136 0.3586 -0.5743 0.03703 -0.3848 0.3885 -0.2612 0.01957 0.1751 -0.3491 0.321 -0.05854 -0.2716 -0.1112 -0.19 -0.258 -0.1576 -0.21 0.01572 0.5461 0.5296 0.2337 -0.2536 0.4973 -0.246 0.1792 0.08305 -0.4828 -0.007954 -0.6817 -0.07472 -0.702 -0.005609 -0.707 0.1391 -0.6944 0.3642 -0.6015 0.6275 -0.253 0.3941 0.2408 -0.4934 0.1225 -0.4678 0.2694 -0.2555 0.389 -0.117 -0.4085 0.316 -0.1561 0.2688 -0.114 -0.234 -0.1335 -0.2529 -0.1575 -0.09523 -0.3728 -0.2317 0.03112 0.07002 0.5562 0.4624 0.3282 -0.2308 0.4643 -0.4138 0.355 -0.009134 -0.03503 0.2844 -0.2779 0.4195 -0.3189 0.4808 -0.1992 0.368 0.038 -0.1098 0.1978 -0.6147 0.1328 -0.3873 0.3639 -0.2082 0.4786 -0.2768 -0.2929 0.2105 -0.2902 0.2958 -0.121 0.2281 -0.1462 -0.2065 -0.1259 -0.2296 -0.1659 -0.2005 -0.2228 -0.05204 -0.385 -0.2771 0.04309 -0.1131 0.5339 0.3871 0.4707 0.1428 0.4324 -0.3477 0.4414 -0.533 0.3754 -0.5441 0.3051 -0.5711 0.2708 -0.5883 0.2652 -0.407 0.3585 -0.1193 0.5688 -0.2342 0.377 -0.331 -0.2804 0.1355 -0.3338 0.3 -0.1523 0.2203 -0.1758 0.2069 -0.1301 -0.1755 -0.1277 -0.1917 -0.1584 -0.2112 -0.1947 -0.1623 -0.2553 -0.01455 -0.3841 -0.209 -0.159 -0.3922 0.292 -0.1193 0.5134 0.1762 0.5547 0.2226 0.5624 0.1554 0.5717 0.09772 0.5905 0.01863 0.5886 -0.1891 0.4332 -0.4181 0.01418 -0.2568 -0.3562 0.1765 -0.3024 0.2786 -0.1863 0.2019 -0.202 0.1944 -0.1618 0.1725 -0.1201 -0.1219 -0.1427 -0.1668 -0.1434 -0.1696 -0.1812 -0.1793 -0.2191 -0.154 -0.2562 -0.00981 -0.3511 0.00569 -0.3594 -0.2114 -0.2023 -0.4123 -0.01746 -0.4789 0.08845 -0.4855 0.09717 -0.4873 0.009834 -0.4405 -0.1557 -0.2497 -0.3138 0.06111 -0.3378 0.2535 -0.2536 0.221 -0.2233 0.1696 -0.226 0.1755 -0.1816 0.1603 -0.1414 0.1192 -0.138 -0.09769 -0.1143 -0.109 -0.1534 -0.1482 -0.1577 -0.1453 -0.1907 -0.137 -0.232 -0.139 -0.2565 -0.07987 -0.2917 0.03069 -0.3354 0.1005 -0.3555 0.11 -0.3516 0.1107 -0.3425 0.1407 -0.3325 0.1894 -0.3114 0.2091 -0.2807 0.1743 -0.2626 0.1346 -0.2582 0.1387 -0.2337 0.1524 -0.1891 0.1351 -0.1592 0.1026 -0.1522 0.1058 -0.1212";



and the codes

mydatanormalized = With[{max = Max@Map[Norm[#] &, mydata, {2}]}, mydata/max];

MatrixPlot[Map[Norm[#] &, mydata, {2}], PlotLegends -> Automatic]

ListVectorDensityPlot[mydata, DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> Full, PlotRangeClipping -> False,
VectorColorFunction -> None,
VectorScaling -> "Linear",
VectorRange -> All, VectorStyle -> Black,
ColorFunctionScaling -> {False, False, False, False, True},
ColorFunction ->
Function[{x, y, vx, vy, n},
Hue[ArcTan[vx, vy]/(2 π), n]],
MaxRecursion -> 2]

ListVectorDensityPlot[mydatanormalized, DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> Full, PlotRangeClipping -> False,
VectorColorFunction -> None,
VectorScaling -> "Linear",
VectorRange -> All, VectorStyle -> Black,
ColorFunctionScaling -> False,
ColorFunction ->
Function[{x, y, vx, vy, n},
Hue[ArcTan[vx, vy]/(2 π), n]],
MaxRecursion -> 2]


However, there is a big difference as shown below. In both plots, spurious color regions (exaggeratedly opaque, I think) show up at the boundary. They are actually outside the data coordinate range, $$[-1.2,1.2]$$. And such distortion looks to suppress the true data in the first plot. However, the original data is nothing special at all in the norm near the boundary (one can easily see by MatrixPlot the norm as shown.)

One can restrict the plot range to the original data coordinates

ListVectorDensityPlot[mydatanormalized, DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}(*,PlotRangeClipping\[Rule]False*),
VectorColorFunction -> None, VectorScaling -> "Linear", VectorRange -> All,
VectorStyle -> Black, ColorFunctionScaling -> False,
ColorFunction -> Function[{x, y, vx, vy, n}, Hue[ArcTan[vx, vy]/(2 π), n]],
MaxRecursion -> 2]


for Method 2 and it gets rid of the spurious region (not shown here), although this is not helpful for Method 1. Here, one has to comment out PlotRangeClipping -> False. However, one needs PlotRangeClipping -> False for good reasons, otherwise

2. arrows at the boundary are cut off, like only half of an arrow is shown

So we still need some remedy/workaround here. How should one correctly plot the data without these weird distortions? (I came up with an idea but weirdly got stuck. Solving that one would do as well.)

Only plot norm: Method 1: Method 2:

• According to Properties & Relations section of document of ListVectorDensityPlot (To be more specific, check the code below the line "Scalar fields can be plotted by themselves with ListDensityPlot"), the density isn't scaled linearly. Have you taken this into consideration? Mar 16, 2021 at 5:52
• @xzczd But I am using my own ColorFunction that scales linearly with the norm. I feel MMA is plotting something wrong here, like a bug. There's just nothing special at the boundary if one looks at the norm plot. In some other cases, I see singular (very bright color) points at the four corners while norms are actually small and nothing really happens there. Mar 16, 2021 at 6:07
• Actually, the four corners (at least three of them) do show similar wrong behavior in the present post. Just compare with the norm plot. Mar 16, 2021 at 6:14
• @xzczd I found some clue. See my update. Mar 16, 2021 at 7:50
• Interesting. It's a blind spot, I should say. I didn't notice ListVectorDensityPlot has drawn a bit more! Mar 16, 2021 at 8:16

I think for both methods, a better solution would be to use RegionFunction to restrict the plotting region:

ListVectorDensityPlot[mydata, DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> Full,
PlotRangeClipping -> False,
RegionFunction -> (-1.2 <= #1 <= 1.2 && -1.2 <= #2 <= 1.2 &),
VectorColorFunction -> None,
VectorScaling -> "Linear",
VectorRange -> All, VectorStyle -> Black,
ColorFunctionScaling -> {False, False, False, False, True},
ColorFunction ->
Function[{x, y, vx, vy, n},
Hue[ArcTan[vx, vy]/(2 π), n]],
MaxRecursion -> 2]


produces

and

ListVectorDensityPlot[mydatanormalized, DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> Full,
PlotRangeClipping -> False,
RegionFunction -> (-1.2 <= #1 <= 1.2 && -1.2 <= #2 <= 1.2 &),
VectorColorFunction -> None,
VectorScaling -> "Linear",
VectorRange -> All, VectorStyle -> Black,
ColorFunctionScaling -> False,
ColorFunction ->
Function[{x, y, vx, vy, n},
Hue[ArcTan[vx, vy]/(2 π), n]],
MaxRecursion -> 2]


produces

In either case, you can remove the thin border around the plotted region using RegionBoundaryStyle -> None.

• Thanks. This doesn't make Method 1 right but indeed improves Method 2. However, it now leaves an awkwardly wider white boundary than the default plot (I can see why). I need to combine/show this plot together with other normal plots with such an outer frame. This one will be conspicuously different. If only we could make it work in the default way... Mar 19, 2021 at 12:18
• I think the reason why methods 1 and 2 don't match is that they actually involve different normalizations: in (1) all norms are computed, their MinMax is seen to be say {MIN,MAX}, and finally when choosing colors for a given norm n the ColorFunction sees (n-MIN)/(MAX-MIN), i.e. all values lie in the range [0,1]; what you do in (2) is divide all vectors by the norm of the vector with largest norm, which means the ColorFunction will see a maximum norm of 1 but a minimum norm greater than 0 (in your case, about 0.0104509 because the null vector is not in your data) Mar 19, 2021 at 13:32
• As for the extra white border, you can remove it using e.g. PlotRangePadding -> -0.03, which for me leaves it essentially as it was before, but this will change the plotting range and I assume it would be equally awkward when sitting next to your other plots. Since you can't trust MMA to make all the right choices by default, my approach in this cases usually involves fixing all of these parameters myself for all of my plots, i.e. standardize plot ranges, range paddings, etc, until all the plots look nicely uniform... Mar 19, 2021 at 13:46
• I take the liberty to edit the code a bit because the structure of mydata is modified. The result isn't influenced. Mar 20, 2021 at 3:50

It is difficult to understand how ListVectorDensityPlot[] works in this case. But for my opinion for some presentation it could be better to use something less rainbow, for instance,

ListVectorDensityPlot[mydatanormalized,
DataRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
PlotRange -> Full, PlotRangeClipping -> False,
VectorColorFunction -> None,
VectorScaling -> "Linear",
VectorRange -> All, VectorStyle -> Black,
ColorFunctionScaling -> False,
ColorFunction ->
Function[{x, y, vx, vy, n},
Hue[ArcCos[vy/Sqrt[vx^2 + vy^2]]/(2 \[Pi]), .5 + n/2]],
MaxRecursion -> 2]


• Thanks. This doesn't remove the spurious colors/regions. Or you just want to show a different ColorFunction? Mar 22, 2021 at 0:56
• @xiaohuamao What do you mean under spurious? In this picture we can see how ListVectorDensityPlot[] interpolating data on the boundary. Mar 22, 2021 at 12:34
• @xiaohuamao If I understand it right, Alex is showing a ColorFunction with which the wild extrapolation of ListVectorDensityPlot isn't that obvious. Mar 26, 2021 at 4:02
• @xzczd Yes, you are right, and also it shows how Mathematica interpolating data on the border. For my opinion it is nice picture. Mar 26, 2021 at 13:06