-1
$\begingroup$

I have a function called $s$ which is a very complicated function of two variables x and y. I want to plot a 2d graph with axes representing x and y parameters and colored points show sign of function $s$ for $x=x_0$ and $y=y_0$ (For example if $s(x_0,y_0)$ is positive it plots a blue point and if not, it plots red point in $(x_0,y_0)$ position of the graph.)

I want $x_0$ to go from $x_1$ to $x_2$ with step equal to $\Delta x$ and $y_0$ to go from $y_1$ to $y_2$ with step equal to $\Delta y$. The reason I want to do that is: Plot3D cannot plot $s(x,y)$, because as I mentioned, it is a very difficult function of x and y. The code I used to plot the function is as below but I don't know how to plot different colors for different signs(nagative and positive):

ListPointPlot3D[Table[s, {x, 0.03, 1, 0.05}, {y, 40, 50, 1}]]

Any answers with an example will be highly appreciated.

$\endgroup$
  • $\begingroup$ ColorFunction will let you use ListPointPlot to plot only red or blue points. ListPointPlot3D[Table[{x, y, Sin[x] Cos[y]}, {x,-Pi,Pi,Pi/16}, {y,-Pi,Pi,Pi/16}], ColorFunction->Function[{x,y,z}, If[z<1/2, Red, Blue]]] but MMA has already chosen a scaled color value by the time it gets to ColorFunction and you can't just say "I want negative z values printed in blue", not unless you want to just use Graphics and you have already said several times you don't want to do that. Perhaps you can try this on your function, adjust the 1/2 if needed and see if it works. Watch out for Complex values though $\endgroup$ – Bill Dec 16 '16 at 16:18
2
$\begingroup$

Let's say your complicated function is Sin[x]Sin[y]

data = Flatten[Table[{x, y, Sin[x] Sin[y]}, {x, 0, 2 Pi, 0.1}, {y, 0, 2 Pi, 0.1}], 1];

ListDensityPlot[data, PlotLegends -> True]

enter image description here

or point by point with your choice of color

Graphics[{If[#[[3]] >= 0, Blue, Red], Point[#[[1 ;; 2]]]} & /@ data, Frame -> True]

enter image description here

$\endgroup$
  • $\begingroup$ Graphics doesn't work in my case. Is there any way that I could use listpointplot? $\endgroup$ – sara nj Dec 16 '16 at 13:49
  • 4
    $\begingroup$ @saranj Please be specific as to why it doesn't work. Do you get specific errors or is there something about your data that can't work with Graphics? $\endgroup$ – JimB Dec 16 '16 at 15:21
0
$\begingroup$

It sounds like you might want DiscretePlot3D:

DiscretePlot3D[
 Sin[8 x] + Cos[x + y],
 {x, 0.03, 1, 0.05}, {y, 40, 50, 1}, 
 ColorFunction -> Function[{x, y, z}, If[z > 0, Blue, Red]],
 ColorFunctionScaling -> False,
 Filling -> None
 ]

Mathematica graphics

Of course, it's basically the same as ListPointPlot3D, with different styling, plus you can reuse the data:

data = Table[{x, y, Sin[8 x] + Cos[x + y]}, {x, 0.03, 1, 0.05}, {y, 40, 50, 1}];
ListPointPlot3D[data,
  ColorFunction -> Function[{x, y, z}, If[z > 0, Blue, Red]],
  ColorFunctionScaling -> False]
]

Mathematica graphics

FWIW, you can get the same computation restrictions on Plot3D, ContourPlot, etc. by choosing PlotPoints and setting MaxRecursion -> 0. Be advised that Plot3D will tweak the plot points off a regular grid slightly.

Plot3D[Sin[8 x] + Cos[x + y], {x, 0.03, 1}, {y, 40, 50}, 
 PlotPoints -> pp, MaxRecursion -> 0, MeshFunctions -> {#3 &}, 
 Mesh -> {{0}}, MeshShading -> {Red, Blue}]

Mathematica graphics

$\endgroup$

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.