Coloring specific points with a different color in Mathematica

久未见 提交于 2019-12-21 09:29:35

问题


The output of the Mathematica command

ListPointPlot3D[
  Join @@ Table[{x, y, 0}, {x, -6, 6, 1}, {y, -6, 6, 1}], 
  PlotStyle -> PointSize[0.02]]

is the following image.

I want to color the points (0,0) and (1,2) with the color red. How do I modify the above command for this?


回答1:


A very simple and straightforward way would be:

list = Join @@ Table[{x, y, 0}, {x, -6, 6, 1}, {y, -6, 6, 1}];
pts = {{0, 0, 0}, {1, 2, 0}};

ListPointPlot3D[{Complement[list, pts], pts}, 
 PlotStyle -> PointSize[0.02]]

Of course, I left it without explicitly specifying colors because the next default color is red. However, if you want to specify your own, you can modify it a little more as:

ListPointPlot3D[{Complement[list, pts], pts}, 
 PlotStyle -> {{Green, #}, {Blue, #}} &@PointSize[0.02]]



回答2:


One could use the ColorFunction option to ListPointPlot3D:

color[0, 0, _] = Red;
color[1, 2, _] = Red;
color[_, _, _] = Blue;

ListPointPlot3D[
  Join @@ Table[{x, y, 0}, {x, -6, 6, 1}, {y, -6, 6, 1}], 
  PlotStyle -> PointSize[0.02],
  ColorFunction -> color, ColorFunctionScaling -> False]

It is important to include the ColorFunctionScaling -> False option because otherwise the x, y and z co-ordinates passed to the colour function will be normalized into the range 0 to 1.

ColorFunction also allows us to define point colouring using arbitrary computations, for example:

color2[x_, y_, _] /; x^2 + y^2 <= 9 = Red;
color2[x_, y_, _] /; Abs[x] == Abs[y] = Green;
color2[_, _, _] = Blue;

ListPointPlot3D[
  Join @@ Table[{x, y, 0}, {x, -6, 6, 1}, {y, -6, 6, 1}], 
  PlotStyle -> PointSize[0.02],
  ColorFunction -> color2, ColorFunctionScaling -> False]




回答3:


yoda shows a fine method. Sometimes however, it is easier to work directly with graphics primitives. Here is an example of that, though in this case I would choose yoda's method.

Graphics3D[{
  PointSize[0.02],
  Point /@ Join @@ Table[{x, y, 0}, {x, -6, 6, 1}, {y, -6, 6, 1}] /. 
    x : _@{1, 2, 0} | _@{0, 0, 0} :> Style[x, Red]
}]


来源:https://stackoverflow.com/questions/7936709/coloring-specific-points-with-a-different-color-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!