3

I'm using Matplotlib to draw surface plots in a 3D space. 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. Also, I want to annotate the x,y,z axis, specifying the name of the values on that axis. 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:

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. So I want to annotate the left corner in the plot. enter image description here

J Freebird
  • 3,664
  • 7
  • 46
  • 81
  • Could you post the code that will generate your plot in its current state? – YXD Nov 30 '13 at 23:04
  • possible duplicate of [Python/matplotlib : plotting a 3d cube, a sphere and a vector?](http://stackoverflow.com/questions/11140163/python-matplotlib-plotting-a-3d-cube-a-sphere-and-a-vector) I'm not sure it will cover all of what you're asking, but I found it useful for a similar problem I had a while back – Iguananaut Nov 30 '13 at 23:13
  • I don't think it's the same question, since here I've already plotted the surface, and I only want to add some annotations to the plot. Thanks. – J Freebird Nov 30 '13 at 23:41
  • To be more specific, if you look at the answer to that question it shows how to make a 3D Arrow and use that to annotate points in a 3D plot--I have used this recipe extensively and it makes me wonder how it isn't built into matplotlib yet. – Iguananaut Dec 12 '13 at 19:54
  • Matplotlib has extensive [annotation support](http://matplotlib.org/examples/pylab_examples/annotation_demo.html). – Benjamin Barenblat Nov 30 '13 at 23:21
  • Unfortunately for whatever reason I don't think annotations are fully supported yet on 3D plots, at least not last I checked. Has that come any further? – Iguananaut Dec 12 '13 at 19:54

1 Answers1

0

Try pylab.annotate() as used here.

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

This says you are using the function incorrectly. Type help(ax.annotate).

Community
  • 1
  • 1
Myles Baker
  • 3,600
  • 2
  • 19
  • 25
  • I got the error "__init__() takes at least 3 arguments (3 given) " for my own problem (in a for loop hoping to annotate indices). code: axarr[1].annotate('%s' % n, n=n, xytext='data') – user391339 Jun 14 '16 at 15:39