1
$\begingroup$

This post is an extension of the post:

How to 2D-plot function in two varibles

Actually I got the answer for that question from

I guess you want to color a list of 2D points using a function like k[s,f]. You can Style the original data and use the resulting data with ListPlot.

But I didn't understand from here:

" If the data is 3D and the third entry is obtained by applying a function like k[s,f] to the first two entries (like data set dt3d below), then the function we use to style the data is slightly different:"

I understand in the example of td and styleddt that in Style[{##} and k[##], that ## refers to the two variables which k is function of them.

But now I try to plot another function, like Y[s,f,d]= s+f+d;, with -1 < s < 1, f= 0.5, and -0.5 < d < 0.5, and I want to plot Y[s,f,d] only at -2 < Y < 0, or Piecewise can be used as in the first example to know Y values.

$\endgroup$

2 Answers 2

1
$\begingroup$
ClearAll[dt, y]
y[s_, d_, f_] := s + f + d;
dt = Transpose[{RandomReal[{-1, 1}, 1000], RandomReal[{-.5, .5}, 1000]}];
styleddt = Style[{##}, PointSize[.02], 
     Piecewise[{{Red, -2 <= y[##, .5] <= 0}},  Blue]] & @@@ dt;
labels = {"-2<=y[s,d,.5]<=0", "  otherwise"};
colors = {Red, Blue};
legend = Row[Style[##, "Panel", 18] & @@@ Transpose[{labels, colors}], Spacer[5]];

ListPlot[styleddt, DataRange -> {{-1, 1}, {-1, 1}}, Frame -> True, 
 ImageSize -> 500, PlotLabel -> legend, 
 FrameLabel -> {Style["s", "Panel", 20], Style["d", "Panel", 20]}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Hi kglr, ` dt ` answer is perfect, but can't we control RandomReal, so as to make the Y-axis scale different than X-axis scale, say on way axis (d) {-0.5,0.5} and on X-axis {0.7,1}. $\endgroup$
    – S.S.
    Apr 24, 2016 at 14:22
  • $\begingroup$ @S.S., i updated with the suggested changes. $\endgroup$
    – kglr
    Apr 24, 2016 at 14:30
  • $\begingroup$ ✩ ✩ ✩ Many thanks. $\endgroup$
    – S.S.
    Apr 24, 2016 at 14:36
1
$\begingroup$

Or ContourPlot

ContourPlot[y[s, 0.5, d], {s, -1, 1}, {d, -.5, .5},
 RegionFunction -> Function[{s, d, z}, -2 < y[s, 0.5, d] < 0],
 Contours -> 10, PlotLegends -> Automatic]

Mathematica graphics


You can use Plot3D and the RegionFunction option.

Plot3D[y[s, 0.5, d], {s, -1, 1}, {d, -.5, .5},
 RegionFunction -> Function[{s, d, z}, -2 < y[s, 0.5, d] < 0]]

Mathematica graphics

Hope this helps.

$\endgroup$
2
  • $\begingroup$ Ya, but I want the plot in 2D, as the first graph in the referred post. $\endgroup$
    – S.S.
    Apr 24, 2016 at 11:33
  • $\begingroup$ @S.S. It seems like overkill. Why not ContourPlot? $\endgroup$
    – Edmund
    Apr 24, 2016 at 11:37

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.