python - 如何在 matplotlib 中标记一个点

标签 python matplotlib plot

我正在使用 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

最佳答案

尝试使用 pylab.annotate() here .

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

这表示您使用的函数不正确。输入 help(ax.annotate)

关于python - 如何在 matplotlib 中标记一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306551/

相关文章:

python - 根据具有特定值的行创建新的 Dataframe 并从原始 Dataframe 中删除行

python - 如何使用 Matplotlib 缩放体素尺寸?

python - asyncio matplotlib show() 仍然卡住程序

r - 以 R 为基数的日期的长轴和短轴刻度

r - 如何强制 R 绘图 y 轴从 y=0 开始并保持颜色?

r - 在 x 轴上绘制带有日期标签的时间序列

python - 使用 PyTorch 的交叉熵损失函数是否需要 One-Hot 编码?

python - 在 true_divide 中遇到被零除 + 在 true_divide 中遇到无效值 + 在 reduce 中遇到无效值

python - matplotlib.pcolor 非常慢。备择方案?

java - jep在java中解释python时出错