在Mathematica中标注多边形的顶点

Dro*_*ror 6 plot wolfram-mathematica

给定平面中的一组点,T={a1,a2,...,an}然后Graphics[Polygon[T]]将绘制由点生成的多边形.如何在多边形的顶点添加标签?只有索引作为标签会比没有更好.有任何想法吗?

Nas*_*ser 9

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
 {{LightGray, Polygon[pts]},
  {pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}]}}
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要加点也

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
 {{LightGray, Polygon[pts]},
  {pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}, {0, -1}]},
  {pts /. {x_, y_} :> {Blue, PointSize[0.02], Point[{x, y}]}}
  }
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新:

使用索引:

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
 {{LightGray, Polygon[pts]},
  {pts /. {x_, y_} :> 
     Text[Style[Position[pts, {x, y}], Red], {x, y}, {0, -1}]}
  }
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Sjo*_*ies 7

Nasser的版本(更新)使用模式匹配.这个使用函数式编程.MapIndexed为您提供坐标及其索引,而无需Position找到它.

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
 {
  {LightGray, Polygon[pts]},
  MapIndexed[Text[Style[#2[[1]], Red], #1, {0, -1}] &, pts]
  }
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者,如果你不喜欢MapIndexed,这里有一个版本Apply(在1级,中缀表示法@@@).

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
idx = Range[Length[pts]];
Graphics[
 {
  {LightGray, Polygon[pts]},
  Text[Style[#2, Red], #1, {0, -1}] & @@@ ({pts, idx}\[Transpose])
  }
 ]
Run Code Online (Sandbox Code Playgroud)

这可以扩展为任意标签,如下所示:

pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
idx = {"One", "Two", "Three"};
Graphics[
 {
  {LightGray, Polygon[pts]},
  Text[Style[#2, Red], #1, {0, -1}] & @@@ ({pts, idx}\[Transpose])
  }
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述