2
$\begingroup$

I am pretty new to mathematica (very new) and am trying to make a plot to display regions of a function that are positive and negative subject to a constraint.

I have a function $P = P(n,m)$ that has positive and negative values in regions of an $(n,m)$ plot. However the function is only valid for some constraint $\sigma=\sigma(n,m)$.

I have tried to do this in two ways but neither really highlights the boundary of where the function takes positive and negative values. The first way is using

ContourPlot[P, {n, 0, 5}, {m, 0, 5}, PlotLegends -> Automatic, FrameLabel -> Automatic, PlotRange -> All, Contours -> 100, RegionFunction -> Function[{n, m}, sigma > 0], ColorFunction -> "GreenPinkTones"]

but doing it this way results in the following which does not really highlight the boundary between positive and negative.

contour plot

The second way I have tried is to use RegionPlot as follows

RegionPlot[{P > 0, P < 0}, {n, 0, 10}, {m, 0, 10}, BoundaryStyle -> {Dashed, Dashed}, PlotStyle -> {Red, Blue}]

In this case though I am not sure how to apply the $\sigma$ constraint of where the plot is valid. It ends up looking like

enter image description here

I think I would probably prefer the contour plot version if I could force the contour colouring to be distinct and normalised around zero but either (or both) methods would be awesome.

Any help here would be much appreciated!!

edit*1

$P$ looks like

enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ Please provide $P$ so we can help you. $\endgroup$ Nov 8, 2018 at 18:05
  • $\begingroup$ @David G. Stork Sure added for one case. I can not add in general what it looks as there are a lot of calculations involved in getting to this. $\endgroup$
    – craigd
    Nov 9, 2018 at 13:48
  • $\begingroup$ What is uzhat and $\sigma$ in your equation. $\endgroup$ Jan 4, 2020 at 14:13

1 Answer 1

1
$\begingroup$
ClearAll[p, sigma]
p[n_, m_] := -(1/(m n (m^2 + n^2)^(5/2) Sqrt[4 + m^2 + n^2])) (2.5 10^-7)
 (32.8634 m^4 - n^2 (4 + n^2) Sqrt[(m^2 + n^2) (4 + m^2 + n^2)] + 
   3 m^2 n^2 (10.9545 - Sqrt[(m^2 + n^2) (4 + m^2 + n^2)])) (9.8696 m n - 
     Cos[Pi (m + n)] Sin[m Pi] Sin[n Pi]);
sigma[n_, m_] := n^2 + m^2; 
  1. You can use MeshFunctions and Mesh to add a mesh line at p[n ,m] == 0:

ContourPlot[p[n, m], {n, 0, 5}, {m, 0, 5}, 
  PlotLegends -> Automatic, FrameLabel -> Automatic, PlotRange -> All, Contours -> 50,
  MeshFunctions -> {p[#, #2] &}, Mesh -> {{0}}, 
  MeshStyle -> Directive[Opacity[1], Red, Thick] , 
  RegionFunction -> (1/16 <= sigma[#, #2] <= 5 &), 
  ColorFunction -> "GreenPinkTones"] 

enter image description here

  1. You can create two ContourPlots (the second one with a single contour at 0) and combine them with Show:

{cp1, cp2} = ContourPlot[p[n, m], {n, 0, 5}, {m, 0, 5}, 
     PlotLegends -> Automatic, FrameLabel -> Automatic, PlotRange -> All, 
     Contours -> #, ContourShading -> #2,
     RegionFunction -> (1/16 <= sigma[#, #2] <= 5 &), 
     ColorFunction -> "GreenPinkTones"] & @@@
   {{30, Automatic}, {{{0, Directive[Opacity[1], Red, Thick]}}, None}};

Show[cp1, cp2]

enter image description here

  1. You can post-process the ContourPlot output to re-style the contour corresponding to 0:

ContourPlot[p[n, m], {n, 0, 5}, {m, 0, 5}, PlotLegends -> Automatic, 
    FrameLabel -> Automatic, PlotRange -> All, Contours -> 50, 
    RegionFunction -> (1/16 <= sigma[#, #2] <= 5 &), 
    ColorFunction -> "GreenPinkTones"] /. Tooltip[{___, l_Line}, 0. | 0] :> 
  Tooltip[{Opacity[1, Red], Thick, l}, 0] 

enter image description here

$\endgroup$
1
  • $\begingroup$ None of these seem to work for me. I just get blank plots I think it might be related to the sigma part in that it is not a defined expression in my case but an evaluated one from growthrate = Solve[(k2 + sigma )*(k2 + (sigma/(nu/kappa)))*(k2) + (N2/(kappa*nu)* kH) == 0, sigma]; growthratearray = growthrate[[All, 1, 2]]; sigma = Max[ Re[growthratearray[[1]]], Re[growthratearray[[2]]]]; I am trying the 1st method using mesh functions and get the warning "sigma equation is not a valid mesh" $\endgroup$
    – craigd
    Nov 19, 2018 at 13:42

Your Answer

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

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