4
$\begingroup$

I want to have a DensityPlot or ListDensityPlot, whose color function is a rainbow but whose luminance is given by another function. For example, I've attached an image of a vortex, whose color function shows the phase and its luminance shows the intensity. In the following plot, you can use F1 = ArcTan[x,y] function with luminance F2 = (x^2+y^2)*Exp[-x^2-y^2].

my example

$\endgroup$
3
  • 1
    $\begingroup$ Could you show the complete code that generated the image you showed? $\endgroup$
    – MarcoB
    Jul 18, 2016 at 0:05
  • 1
    $\begingroup$ You've seen Hue[], right? $\endgroup$ Jul 18, 2016 at 0:07
  • $\begingroup$ Yes I sow it, but I can use that only for Plot3D (see Below the code). I want the same dependence but for DensityPlot and more for ListDensityPlot. Plot3D[(x^2 + y^2) Exp[-x^2 - y^2], {x, -3, 3}, {y, -3, 3}, PlotPoints -> 50, ColorFunction -> Function[{x, y, z}, Hue[Rescale[ArcTan[x, y], {-Pi, Pi}]]], ColorFunctionScaling -> False, PlotLegends -> Automatic] $\endgroup$
    – Mushegh
    Jul 18, 2016 at 11:31

2 Answers 2

4
$\begingroup$

Edit

Table Case

Kuba, can you add also a solution for tables instead of functions, because in fact I have something like this: table1 = Table[(x^2 + y^2) Exp[-x^2 - y^2], {x, -3, 3}, {y, -3, 3}], table2 = Table[ArcTan[x, y], {x, -2.999, 3}, {y, -3., 3}]

– Mushegh

{
    table1 = Table[(x^2 + y^2) Exp[-x^2 - y^2], {x, ##}, {y, ##}],
    table2 = Table[Arg[x + I y], {x, ##}, {y, ##}]
} & @@ {-3, 3, .2};

Image[
  Reverse[#, 2] & @ Transpose @ MapThread[
    {(#2 - Pi/2.)/Pi, 1, # E } &,
    ArrayResample[#, {500, 500}] & /@ N[{table1, table2}],
    2
  ],
  ColorSpace -> "HSB"
]

enter image description here

You have probably noticed a strange "outflow". That is because of the interpolation of positive "x" axis neighborhood for ArcTan[x,y]:

table2 // ListPlot3D

enter image description here

Here is a fix:

res = ArrayResample[N@#, {500, 500}, ##2] &;
Image[
 Reverse[#, 2] &@Transpose@MapThread[
    {Which[#3 > 0, ArcSin[#2], #2 > 0, ArcCos[#2] + Pi/2, True, 
         ArcCos[#2] - 3 Pi/2]/Pi - 1/2., 1, # E} &,
    {
     res@table1,
     res[Sin@table2],
     res[Cos@table2]
     },
    2
    ],
 ColorSpace -> "HSB"
 ]

enter image description here


Functions case

plot = ParametricPlot[{x, y}, {x, -#, #}, {y, -#, #},
     ColorFunction        -> Function[{x, y, u, v},
        Directive[
           Opacity[E (x^2 + y^2)*Exp[-x^2 - y^2]], (*E - scaling factor*)
           Hue[(ArcTan[x, y] - Pi/2)/Pi]
        ]
     ],
     PlotPoints           -> 100,
     ColorFunctionScaling -> False,
     Axes                 -> False,
     Frame                -> False,
     Background           -> Black,
     BoundaryStyle        -> None,
     ImageSize            -> {Automatic, 300},
     Mesh                 -> None
     ] &@3

barLegend = ParametricPlot[{x, y}, {x, 0, .1}, {y, 0, 2 Pi}, 
   ColorFunction    -> (Hue[#2] &), 
   AspectRatio      -> 10, 
   PlotRangePadding -> None, 
   FrameTicks       -> {{False, {0, 2 Pi}}, {False, False}}, 
   BaseStyle        -> 24,
   FrameLabel       -> {{None, "Phase"}, {None, None}}, 
   ImageSize        -> {Automatic, 300},
   Mesh             -> None
]


Grid[{{plot, barLegend}}]

enter image description here

$\endgroup$
12
  • $\begingroup$ the "BaseStyle" and "Ticks" are not working. Is it problem of Mathematica version I have? it is Mathematica 9.0 $\endgroup$
    – Mushegh
    Jul 19, 2016 at 12:29
  • $\begingroup$ Also the color bar is discrete colors from orange to red $\endgroup$
    – Mushegh
    Jul 19, 2016 at 12:38
  • $\begingroup$ @Mushegh I've changed BarLegend so Ticks should be correct now. What do you mean in your second comment? $\endgroup$
    – Kuba
    Jul 19, 2016 at 12:47
  • $\begingroup$ @Mushegh, Hue[0] and Hue[1] are both red. $\endgroup$ Jul 19, 2016 at 13:01
  • $\begingroup$ Now it is working, I have added "Mesh-> None", because it was bringing some mesh on the plot. Thanks $\endgroup$
    – Mushegh
    Jul 19, 2016 at 13:54
2
$\begingroup$

No legend for this slight simplification of Kuba's proposal, but it can be easily added if desired:

RegionPlot[True, {x, -3, 3}, {y, -3, 3}, Background -> Black, BoundaryStyle -> None, 
           ColorFunction -> Function[{x, y}, Hue[(Arg[x + I y] - Pi/2)/Pi, 1, 1,
                                                 (x^2 + y^2) Exp[1 - x^2 - y^2]]], 
           ColorFunctionScaling -> False, Frame -> False, PlotPoints -> 95]

it does look a bit hypnotic

$\endgroup$
6
  • $\begingroup$ Thank you for short solution, can you add also a solution for tables instead of functions, because in fact I have something like this: table1 = Table[(x^2 + y^2) Exp[-x^2 - y^2], {x, -3, 3}, {y, -3, 3}] table2 = Table[ArcTan[x, y], {x, -2.999, 3}, {y, -3., 3}] $\endgroup$
    – Mushegh
    Jul 19, 2016 at 23:27
  • $\begingroup$ If I am trying to use interpolation and after your option, the plots are loosing the quality. $\endgroup$
    – Mushegh
    Jul 19, 2016 at 23:27
  • 2
    $\begingroup$ @Mush, why did you not post that problem to begin with?! $\endgroup$ Jul 19, 2016 at 23:31
  • $\begingroup$ In the begining I thougth that knowing the solution for functions is enough, because I can after interpolate the data to a function, but then I have tried it and sow that the quality is very bad by interpolation option $\endgroup$
    – Mushegh
    Jul 20, 2016 at 12:57
  • $\begingroup$ Actually I have this option, can you comment to it? $\endgroup$
    – Mushegh
    Jul 20, 2016 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.