TransWikia.com

Make a density plot from a file where the data makes a shape with a hole in it

Mathematica Asked by maggie on December 17, 2020

I have a .dat file and I want to make preferably a density (or surface) plot with it.

ListDensityPlot[f, ColorFunction -> "Rainbow", Axes -> False, 
 Background -> Black, 
 PlotLegends -> 
  Placed[BarLegend[Automatic, LegendMargins -> {{0, 0}, {10, 6}}, 
    LegendMarkerSize -> {15, 300}, LegendLabel -> "F"], After] , 
 AspectRatio -> 0.5,  PlotRange -> All]

The data has the shape of a not perfectly round donut seen from above, so there’s a hole inside and the density and surface plot commands apply the color of the ColorFunction as if it’s 0 to the part in the center that corresponds to the hole.
Is there a way that the part in the middle doesn’t get colored? Or can I assign specifically black to the values that are "0" (but where in reality there’s not data) and still use the Color Function?

2 Answers

You may try the option"RegionFunction" of ListDensityPlot. Here is an example:

dat = Table[t1 = RandomReal[{-1, 1}]; t2 = RandomReal[{-1, 1}]; 
   If[t1^2 + t2^2 < .5, Nothing[], {t1, t2, t1^2 + t2^2}], 1000];
ListDensityPlot[dat, RegionFunction -> ((#1^2 + #2^2 >= .6) &)]

enter image description here

But note, RegionFunction together with ListDensity plot is a bit finicky. It seems that it only works if at least a few points are excluded. E.g. in the above, setting the limit to 0.5:RegionFunction -> ((#1^2 + #2^2 >= .5) &) will not work.

Answered by Daniel Huber on December 17, 2020

You can design a ColorFunction so that a standard color function is used in most cases, except for some values. Here is a possiblity.

Case 1: The standard "rainbow" function

h = Table[Sin[j^2 + i], {i, 0, Pi, Pi/4}, {j, 0, Pi, Pi/4}];
l1 = ListDensityPlot[h, ColorFunction -> ColorData["Rainbow"], 
  Axes -> False, Background -> LightGray, AspectRatio -> 0.5, 
  PlotRange -> All]

enter image description here Case 2: As Case 1, but returns Black for value 0

h = Table[Sin[j^2 + i], {i, 0, Pi, Pi/4}, {j, 0, Pi, Pi/4}];
col[c_] := Black;
l2 = ListDensityPlot[h, 
  ColorFunction -> (If[# != 0., ColorData["Rainbow"][#] &, col[#] &]),
   Axes -> False, Background -> LightGray, AspectRatio -> 0.5, 
  PlotRange -> All]

enter image description here Case 3: As Case 2, but returns Black with Opacity 0 for value 0

h = Table[Sin[j^2 + i], {i, 0, Pi, Pi/4}, {j, 0, Pi, Pi/4}];
col[c_] := {Opacity[0], Black};
l3 = ListDensityPlot[h, 
  ColorFunction -> (If[# != 0., ColorData["Rainbow"][#] &, col[#] &]),
   Axes -> False, Background -> LightGray, AspectRatio -> 0.5, 
  PlotRange -> All]

enter image description here

Answered by Jean-Pierre on December 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP