0
$\begingroup$

Let's say we have function $g(\zeta)$ and another map $f(\zeta)$, where $\zeta$ is a complex variable. I am trying to plot a graph of $f(\zeta)$ but given the condition of $g(\zeta)$.

For instance, I am trying to have a command like: $$\text{plot}\ f(\zeta)\ \text{blue if}\ g(\zeta)\leq 1$$

A similar question has been asked here: Conditional options in Plot

I followed the answer in that question but I cannot get what I want. Here is what I have tried:

  1. Firstly, I define the conditional plot command, as we are dealing with two variables $(\zeta=x+iy)$, I changed "plot" in the original command to contour plot.

    ConditionalPlot[fun_,condition_, varrange_, trueopts_, falseopts_] :=    
      Module[{plottrue, plotfalse}, 
        plottrue = ContourPlot[If[condition, func], varrange, trueopts];
        plotfalse = ContourPlot[If[Not[condition], func], varrange, falseopts]; 
        Show[plottrue, plotfalse, PlotRange -> All]]
    
  2. Then, I used this command to plot:

    ConditionalPlot[f (x + Iy), g (x + Iy) <= 1, {x, -3, 3}, {y, -3, 3},    
      {Filling -> Axis, FillingStyle -> LightGreen, PlotStyle -> Dashed}, 
      {Filling -> Axis,FillingStyle -> LightRed, PlotStyle -> Thick}]
    

However, the output of Mathematica is just the code I have entered, without producing the graph. I don't really know where the problem is.

I also tried to use explicit formula of f(x+Iy) and g(x+Iy) and the problem is still there.

I also tried this command:

ConditionalPlot[f (x + Iy),g (x + Iy) <= 1, {x, -3, 3}, {y, -3, 3}, 
  {PlotStyle -> Dashed},{PlotStyle -> Thick}] 

since I think maybe Filling--Axis is not right for contour plot, however it still cannot work.

Any idea is welcome! Thank you!

I also tried another way to do this question

The idea is from this post: Plot 3D with conditions and variables dependent on each other

Here is my command:

ContourPlot[
  ConditionalExpression[f (x + Iy), g (x + Iy) <= 3 && g (x + Iy) >= 1], 
  {x, -3, 3}, {y, -3, 3}, 
  PlotPoints -> 100, MaxRecursion -> 4]

However, Mathematica then produced me a graph with nothing. Just empty. So I think there is something it cannot understand...

$\endgroup$
2
$\begingroup$

Edit

There are quite a few minor errors in your code and, I think, two major ones:

  • f and g have to be real-valued functions of a complex variable.
  • you should be plotting f with Plot3D if you want to use options like Filling.

I also think that for Plot3D, the filling option Filling -> Bottom will give a better plot than Filling -> Axis.

Taking the above into account and fixing the minor errors as well, produces the following:

Clear[ConditionalPlot]
ConditionalPlot[func_, condition_, xrng_, yrng_, trueopts_, falseopts_] := 
  Module[{plottrue, plotfalse}, 
    plottrue = Plot3D[If[condition, func], xrng, yrng, trueopts];
    plotfalse = Plot3D[If[Not[condition], func], xrng, yrng, falseopts];
    Show[plottrue, plotfalse, PlotRange -> All]]

f[z_] := Re[z^2]
g[z_] := Im[z]

ConditionalPlot[f[x + I y], g[x + I y] <= 1, {x, -3, 3}, {y, -3, 3}, 
  {Filling -> Bottom, FillingStyle -> LightGreen, 
   PlotStyle -> Lighter[Green, .2], Lighting -> "Neutral"},
  {Filling -> Bottom, FillingStyle -> LightRed, 
   PlotStyle -> Lighter[Red, .2], Lighting -> "Neutral"}]

plot

As to the matter you bring up in your comment this answer, what you suggest will work.

Clear[ConditionalPlot]
ConditionalPlot[func_, condition_, xrng_, yrng_, opts_] :=
  Module[{plottrue},
    plottrue = Plot3D[If[condition, func], xrng, yrng, opts];
    Show[plottrue]]

ConditionalPlot[f[x + I y], g[x + I y] <= 1, {x, -3, 3}, {y, -3, 3}, 
  {Filling -> Bottom, FillingStyle -> LightGreen, 
   PlotStyle -> Lighter[Green, .2], Lighting -> "Neutral"}]

plot2

But don't think that now the use of Module and Show are overkill? Wouldn't the following, which gives the same plot, be better?

Clear[ConditionalPlot]
ConditionalPlot[func_, condition_, xrng_, yrng_, opts_] :=
  Plot3D[If[condition, func], xrng, yrng, opts]
$\endgroup$
  • $\begingroup$ Amazing!!! I have an another question that, what should I do if I want Mathematica do nothing if the condition is false? $\endgroup$ – JacobsonRadical Feb 13 at 1:57
  • $\begingroup$ Is that okay if I just remove plotfalse in the show command? $\endgroup$ – JacobsonRadical Feb 13 at 1:59
  • $\begingroup$ @JacobsonRadical. Does my edit address the issue you raise in your comments? $\endgroup$ – m_goldberg Feb 13 at 15:45
  • $\begingroup$ Yes! Thank you so much!! $\endgroup$ – JacobsonRadical Feb 20 at 19:06

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.