0
$\begingroup$

I tried to use contourplot to a 2D fucntion with rescaled color, the code is given below

Needs["PlotLegends`"]
Num=1000;
Delta=2*3.1415926/Num;/
(*Three components of the function*)
BX[R_,X_,Y_,Z_]:=Module[{r=R,x=X,y=Y,z=Z,dum=X^2+Y^2+Z^2},r*Sum[z*Cos[k*Delta]*Delta/(dum-2*x*r*Cos[k*Delta]-2*y*r*Sin[k*Delta]+r^2)^(3/2),{k,0,Num}]] 
BY[R_,X_,Y_,Z_]:=Module[{r=R,x=X,y=Y,z=Z,dum=X^2+Y^2+Z^2},r*Sum[z*Sin[n*Delta]*Delta/(dum-2*x*r*Cos[n*Delta]-2*y*r*Sin[n*Delta]+r^2)^(3/2),{n,0,Num}]]
BZ[R_,X_,Y_,Z_]:=Module[{r=R,x=X,y=Y,z=Z,dum=X^2+Y^2+Z^2},r*Sum[(-x*Cos[n*Delta]-y*Sin[n*Delta]+r)*Delta/(dum-2*x*r*Cos[n*Delta]-2*y*r*Sin[n*Delta]+r^2)^(3/2),{n,0,Num}]]
(*The function is given below*)
I1 = 0.1;
I2 = -0.12;
R1 = 1;
R2 = 1.4;
B[X_, Z_] := 
 Module[{x = X, z = Z}, 
   Sqrt[(I1*BX[R1, x, 0, z] + 
    I2*BX[R2, x, 0, z])^2 + (I1*BY[R1, x, 0, z] + 
    I2*BY[R2, x, 0, z])^2 + (I1*BZ[R1, x, 0, z] + 
    I2*BZ[R2, x, 0, z])^2]] // N
(*Plot*)
ShowLegend[
 ContourPlot[B[x, z], {x, -1, 1}, {z, 0.4, 1}, Contours -> 50, 
  ColorFunctionScaling -> False, 
  ColorFunction -> (ColorData["Rainbow"][
  Rescale[#, {0, 5}]] &)], {ColorData["Rainbow"][1 - #1] &, 20, 
  "5", "0"}]

However the result does not make any sense, I expected a range of color same as the legend given. the result is given below:

enter image description here

Looks like 0 everywhere, but, if you check the function value, you will see this is wrong.

any ideas?

$\endgroup$

closed as off-topic by Jason B., user9660, m_goldberg, MarcoB, Mr.Wizard Mar 1 '16 at 2:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jason B., Community, m_goldberg, MarcoB, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ What version of Mathematica are you using? The PlotLegends` package was deprecated in v9 in favor of the built-in option PlotLegends which is superior in many ways. $\endgroup$ – rcollyer Mar 23 '15 at 14:21
  • $\begingroup$ @rcollyer, v7, I really do not care much about the legend. My concern is the color, how can I get as much color as possible on the plotting? and am I right for the rescale part? $\endgroup$ – Hang Yang Mar 23 '15 at 14:26
  • $\begingroup$ If you remove the manual rescaling and let ContourPlot do it, it usually will give you a lot more variation. I tend to manually scale things only when they have to match across multiple plots. $\endgroup$ – rcollyer Mar 23 '15 at 14:35
  • $\begingroup$ @rcollyer, if you input $ColorData["Rainbow"]$, it will output {0,1} plus a color bar, does that mean the color on the color bar mapping with the value between 0 and 1? $\endgroup$ – Hang Yang Mar 23 '15 at 14:43
  • $\begingroup$ Yes, ContourPlot scales from 0 to 1, by default. I'd let ContourPlot do its thing and then supply ShowLegend with the correct scale. $\endgroup$ – rcollyer Mar 23 '15 at 14:47
1
$\begingroup$

Your problem was that you were rescaling numbers that run between 0 and 0.4 (the actual range of output for B over this range) as if they ran between 0 and 5.

Range[0, 0.4, .1]
Rescale[%, {0, 5}]
(* {0., 0.1, 0.2, 0.3, 0.4} *)
(* {0., 0.02, 0.04, 0.06, 0.08} *)

If you want to manually set your own color function, then you should find the range of values the function takes before rescaling,

{min, \max} = {NMinimize[{B[x, z], -1 <= x <= 1 && 0.4 <= z <= 1}, {x, z}],
    NMaximize[{B[x, z], -1 <= x <= 1 && 0.4 <= z <= 1}, {x, z}]}[[All, 1]]
(* {7.17192*10^-11, 0.380612} *)

Then just plot like

ContourPlot[B[x, z], {x, -1, 1}, {z, 0.4, 1}, Contours -> 50, 
 ColorFunctionScaling -> False, 
 ColorFunction -> (ColorData["Rainbow"][Rescale[#, {min, max}]] &), 
 PlotRange -> All]

enter image description here

But as others have said, you get the same plot if you just let the system choose the color function,

ContourPlot[B[x, z], {x, -1, 1}, {z, 0.4, 1}, Contours -> 50, 
 ColorFunction -> "Rainbow", PlotRange -> All]
$\endgroup$

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