Skip to content
Advertisement

How can I label my axis after it print out all the data and mark for each data?

The coding below is my code. I would like to label the axis after it print all the data and the mark for each data. I don’t know why the label name only display at the begin.

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

np.set_printoptions(threshold=np.inf)
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

def init():
   global points
   points = np.loadtxt('Surface.txt')

def animate(i):
   ax1.clear()
   ax1.plot(points[:i, 0], points[:i, 1], points[:i, 2])

ani = animation.FuncAnimation(fig, animate, init_func=init, interval = 1000)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')

plt.show()

The content of the Surface.txt is

0  0  3
1  1  4
2  4  8
4 16  6
5 25  5
6 36  3

Advertisement

Answer

You can accomplish that by also adding the set_label commands inside the def animate() function like this

def animate(i):
   ax1.clear()
   ax1.plot(points[:i, 0], points[:i, 1], points[:i, 2])
   ax1.set_xlabel('x')
   ax1.set_ylabel('y')
   ax1.set_zlabel('z')

So the final program will look like shown below :

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

np.set_printoptions(threshold=np.inf)
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

def init():
   global points
   points = np.loadtxt('Surface.txt')

def animate(i):
   ax1.clear()
   ax1.plot(points[:i, 0], points[:i, 1], points[:i, 2])
   ax1.set_xlabel('x')
   ax1.set_ylabel('y')
   ax1.set_zlabel('z')

ani = animation.FuncAnimation(fig, animate, init_func=init, interval = 1000)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')

plt.show()

Advertisement