4
$\begingroup$

Let's create some sample data

n = 100;
d00 = Table[{RandomReal[{-1, 1}], RandomReal[{-4, 4}], RandomReal[{-1, 1}]}, {i, 1, n}];

Now I want the following: create a list plot with the first element of the list as $x$ coordinate and the third element as the $y$ coordinate.

d0 = Table[{d00[[i, 1]], d00[[i, 3]]}, {i, 1, Length[d00]}];

The color of each point is controlled by the corresponding second element. In particular, if the real number is between -2 and 2 the point should be green, while if it is lower/equal than -2 or greater/equal than 2 the point should be red.

Any suggestions?

$\endgroup$
0

5 Answers 5

4
$\begingroup$

Here's another option:

l = RandomReal[{-4, 4}, {200, 3}];

 ListPlot[
 List /@ (l[[All, {1, 3}]])
 , PlotStyle -> (If[Abs[#[[2]]] < 2, Green, Red] & /@ l)
 ]

Where you can use any function that returns a color in your styling. You might want have to rescale your data if it's not in the range {0,1}.

enter image description here

If you want to change point sizes you can use Directive

ListPlot[
 List /@ (l[[All, {1, 3}]])
 , PlotStyle -> (If[Abs[#[[2]]] < 2, 
      Directive[Green, PointSize[0.04]], Red] & /@ l)
 ]

This gives:

enter image description here

$\endgroup$
5
$\begingroup$

Basically a duplicate of this question.

You can just style each point before you pass them to ListPlot for things like this.

Define a color function:

cfun = Piecewise[{{White, # <= -2}, {Green, -2 < # < 2}, {Red, # > 2}}, Black] &

Then style by second element:

d0 = Table[Style[{d00[[i, 1]], d00[[i, 3]]}, cfun[d00[[i, 2]]]], {i, 1, Length[d00]}];

ListPlot[d0, Background -> Lighter[Gray, 0.5]]

enter image description here

Also Specifying PlotMarkers

Using the PlotMarkers option will mask the Style definitions, unless you specify the undocumented:

Method -> {"OptimizePlotMarkers" -> False}

Like so:

GraphicsRow[{
  ListPlot[d0, PlotMarkers -> {Automatic, 5}],
  ListPlot[d0, PlotMarkers -> {Automatic, 5}, Method -> {"OptimizePlotMarkers" -> False}]
}]

enter image description here

$\endgroup$
5
  • $\begingroup$ Nice! And if I want to control the size of the points? $\endgroup$
    – Vaggelis_Z
    May 5, 2016 at 20:03
  • $\begingroup$ Heh, yeah, using the PlotMarkers option will mask the Style specifications. Instead you can use PlotStyle -> PointSize[0.03] $\endgroup$ May 5, 2016 at 20:13
  • $\begingroup$ If I use ListPlot[d0, PlotStyle -> PointSize[0.02]] then around each red and green dot there is a blue circle! $\endgroup$
    – Vaggelis_Z
    May 5, 2016 at 20:15
  • $\begingroup$ How strange, I don't see that. Regardless, a more robust fix appears to be to set Method -> {"OptimizePlotMarkers" -> False} then you can actually use PlotMarkers as normal. $\endgroup$ May 5, 2016 at 20:16
  • $\begingroup$ Now you lost me! Can you make an edit? $\endgroup$
    – Vaggelis_Z
    May 5, 2016 at 20:18
2
$\begingroup$

Just some variants:

l = RandomReal[{-4, 4}, {200, 3}];
With[{g = #[[All, {1, 3}]] & /@ GatherBy[l, #[[2]] < 2 &]},
 ListPlot[g, PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]]
ListPlot[Last@Reap[Sow[{#1, #3}, #2 < 2] & @@@ l, _, #2 &], 
 PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]
ListPlot[GroupBy[l, #[[2]] < 2 &, #[[All, {1, 3}]] &], 
 PlotStyle -> {Red, Green}, PlotMarkers -> {Automatic, 8}]

Outputs:

enter image description here

$\endgroup$
2
$\begingroup$

Another way is to use Graphics which gives you more control over your plot.

n = 100;
d00 = Table[{RandomReal[{-1, 1}], RandomReal[{-4, 4}], 
   RandomReal[{-1, 1}]}, {i, 1, n}];


{x1, x2} = {Min[#], Max[#]} &@d00[[All, 2]];
col[x_] := If[Abs[x] < 2, Green, Red]  (*for color*)
scale[x_] := (x - x1)/(x2 - x1)/20     (*for point size*)
Graphics[{col[#[[2]]], Disk[#[[{1, 3}]], scale[#[[2]]]]} & /@ d00, Frame -> True]

enter image description here

$\endgroup$
1
$\begingroup$

ListPlot with styled data

styleddata1 = With[{ps = Rescale[#2, Through[{Min, Max}@d00[[All, 2]]], {5, 20}]},
    Style[Tooltip[{#, #3}, #2], If[-2 <= #2 <= 2, Directive[Green, AbsolutePointSize[ps]], 
       Directive[Red, AbsolutePointSize[ps]]]]] & @@@ d00;

ListPlot[styleddata1, AspectRatio -> 1]

Mathematica graphics

BubbleChart with styled data

styleddata2 = Style[Tooltip[{#, #3,
         Rescale[#2, Through[{Min, Max}@d00[[All, 2]]], {0.05, 1}]}, #2], 
         If[-2 <= #2 <= 2, Green, Red]] & @@@ d00;

BubbleChart[styleddata2]

Mathematica graphics

$\endgroup$

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