简体   繁体   English

如何在matplotlib中标记一个点

[英]how to mark a point in matplotlib

I'm using Matplotlib to draw surface plots in a 3D space. 我正在使用Matplotlib在3D空间中绘制表面图。 To make the plot more readable, I want to mark the lowest point in the surface(I have already found the x,y coordinate using the data), show its x,y,z value. 为了使图更易读,我想标记曲面中的最低点(我已经使用数据找到了x,y坐标),显示其x,y,z值。 Also, I want to annotate the x,y,z axis, specifying the name of the values on that axis. 另外,我想注释x,y,z轴,指定该轴上的值的名称。 Anybody knows how to do it? 谁知道怎么做? Thanks a lot. 非常感谢。

Here's the code(I'm reading the error rate data from a file): 这是代码(我正在从文件中读取错误率数据):

import numpy as np
import csv
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def readdata(filename):
   reader = np.loadtxt(filename, dtype = np.ndarray, delimiter=',')
   return reader

fig = plt.figure()
ax = fig.gca(projection='3d')
Z = readdata('continuedData')
for i in range(len(Z)):
    Z[i] = float(Z[i])
Z = np.reshape(Z,(15,15))
X = np.arange(0.011, 0.026, 0.001)
Y = np.arange(0.11, 0.25, 0.01)
X, Y = np.meshgrid(Y, X)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
      linewidth=0, antialiased=False)
ax.set_ylabel('learningRate')
ax.set_xlabel('momentum')
ax.set_zlabel('error rate')
ax.annotate('lowestPoint', xyz=(0.011,0.11,1.78199), xycoords='data',xytext=(10,10),
         textcoords='offset points', arrowprops=dict(arrowstyle="->"))
plt.show()

I'm trying to do it but there's an error with ax.annotate: 我正在尝试这样做但是ax.annotate有一个错误:

Traceback (most recent call last):
File "/home/yongfeng/workspace/ANN/plotContinuedData.py", line 25, in <module>
ax.annotate('lowestPoint', xyz=(0.011,0.11,1.78199), xycoords='data',xytext=         (10,10),textcoords='offset points', arrowprops=dict(arrowstyle="->"))
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 3348, in annotate
a = mtext.Annotation(*args, **kwargs)
TypeError: __init__() takes at least 3 arguments (6 given)

Actually I don't know if I need to specify the Z value as well or the function does it automatically. 实际上我不知道是否需要指定Z值或者函数自动执行。 So I want to annotate the left corner in the plot. 所以我想注释情节中的左角。 在此输入图像描述

Try pylab.annotate() as used here . 试试这里使用的pylab.annotate()

TypeError: __init__() takes at least 3 arguments (6 given)

This says you are using the function incorrectly. 这表示您使用的功能不正确。 Type help(ax.annotate) . 输入help(ax.annotate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM