如何在Matplotlib中标记一个点

3

我正在使用Matplotlib在3D空间中绘制表面图。为了使图形更易读,我想标记表面中的最低点(已经找到了x,y坐标),显示其x,y,z值。此外,我还想注释x,y,z轴,指定该轴上值的名称。有人知道如何做吗?非常感谢。

以下是代码(我正在从文件中读取错误率数据):

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()

我正在尝试做这件事,但是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)

我其实不知道是否需要指定Z值,还是函数会自动处理。因此,我想在图中注释左上角。

enter image description here

你可以发一下代码生成当前状态的绘图吗? - YXD
可能是Python/matplotlib:绘制3D立方体、球体和向量?的重复问题。我不确定它是否涵盖了您所问的所有内容,但我发现它对我以前遇到的类似问题很有用。 - Iguananaut
Matplotlib拥有广泛的注释支持 - Benjamin Barenblat
我认为这不是同一个问题,因为在这里我已经绘制了表面,我只想向图中添加一些注释。谢谢。 - J Freebird
更具体地说,如果你看一下那个问题的答案,它展示了如何制作一个三维箭头,并用它来注释三维图中的点——我已经广泛使用了这个技巧,让我想知道为什么它还没有被内置到matplotlib中。 - Iguananaut
不幸的是,出于某种原因,我认为注释在3D图上尚未得到完全支持,至少在我上次检查时还没有。这方面有进展吗? - Iguananaut
1个回答

0
尝试使用 pylab.annotate(),就像 这里 使用的那样。
TypeError: __init__() takes at least 3 arguments (6 given)

这表示您正在错误地使用该函数。输入help(ax.annotate)以获取帮助。


我在自己的问题中遇到了错误 "init() takes at least 3 arguments (3 given)"(init()至少需要3个参数,但给出了3个),代码如下:axarr[1].annotate('%s' % n, n=n, xytext='data')。这是在一个for循环中希望注释索引时发生的。 - user391339

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接