标记数据点

时间:2016-11-02 14:20:26

标签: matplotlib

我正在尝试使用 3 格式的电源标记数据点,即24^3。 例如,

A = [1,2,3,4,5]
B = [100,200,300,400,500]

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(A,B)
n=[24,48,96,192]
for i, txt in enumerate(n):
   ax.annotate(txt, (A[i],B[i]))
plt.show()

enter image description here

所以,我希望n=[24,48,96,192]代替24^3,48^3,96^3,而不是import play.api.libs.json.{JsValue, Json, Writes} sealed trait Model case class User(name: String, age: String, address: String) extends Model object User { implicit val userWrites = Json.writes[User] } case class CallLog(timestamp: String, status_of_call: String) extends Model object CallLog { implicit val callLogWrites = Json.writes[CallLog] } implicit val modelWrites = new Writes[Model] { override def writes(o: Model): JsValue = o match { case u: User => Json.toJson(u) case cl: CallLog => Json.toJson(cl) } } def helper(models: Model*): Either[JsValue, String] = models match { case Nil => Right("Error") case _ => Left(Json.toJson(models)) } helper(User("John", "32", "...")) helper(User("John", "32", "..."), CallLog("now", "In progress")) ,如果我可以使用改进的表示形式来表示3的力量,那么我会更好。{/ p>

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

请参阅:http://matplotlib.org/users/mathtext.html

示例:

import matplotlib.pylab as plt

A = [1,2,3,4,5]
B = [100,200,300,400,500]

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(A,B)
n=[24,48,96,192]
for i, txt in enumerate(n):
   ax.annotate(r'${}^3$'.format(txt), (A[i],B[i]))
plt.show()

enter image description here

答案 1 :(得分:0)

您的注释通话只需要进行一些小改动:

for i, txt in enumerate(n):
    ax.annotate(str(txt) + '$^3$', (A[i],B[i]))

使用'$ ... $',您可以输入某种数学模式。 在此处查看更多详细信息:http://matplotlib.org/users/mathtext.html

结果应如下所示: enter image description here