将标签添加到Matplotlib中的3D图线。

发布于 2025-02-09 16:01:10 字数 2117 浏览 2 评论 0原文

我正在尝试使用matplotlib.Animation将标签添加到动画3D绘图线中。我正在使用的示例代码基于此示例a> 编辑:完整的代码在这里:

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

# Fixing random state for reproducibility
np.random.seed(19680801)


def Gen_RandLine(length, dims=2):
"""
Create a line using a random walk algorithm

length is the number of points for the line.
dims is the number of dimensions the line has.
"""
lineData = np.empty((dims, length))
lineData[:, 0] = np.random.rand(dims)
for index in range(1, length):
    # scaling the random numbers by 0.1 so
    # movement is small compared to position.
    # subtraction by 0.5 is to change the range to [-0.5, 0.5]
    # to allow a line to move backwards.
    step = ((np.random.rand(dims) - 0.5) * 0.1)
    lineData[:, index] = lineData[:, index - 1] + step

return lineData


def update_lines(num, dataLines, lines):
    for line, data in zip(lines, dataLines):
    # NOTE: there is no .set_data() for 3 dim data...
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2, :num])
    return lines

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Four lines of random 3-D lines
data = [Gen_RandLine(25, 3) for index in range(4)]

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for 
dat in data]

# Setting the axes properties
ax.set_xlim3d([0.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 1.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs= 
   (data, lines),
                               interval=50, blit=False)

plt.show()

'''

这是使用随机生成的点在3D中绘制一系列行。

我已经将绘制的动画线的数量减少到4,我想将标签(a,b,c,d)放在线路的原点上。我已经看过API,但只能看到如何标记轴和刻度,而不是绘制的数据。

I'm trying to add labels to animated 3D plotlines using matplotlib.animation. The sample code I am using is based on this example from the matplotlib example files
EDIT: full code is here:

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

# Fixing random state for reproducibility
np.random.seed(19680801)


def Gen_RandLine(length, dims=2):
"""
Create a line using a random walk algorithm

length is the number of points for the line.
dims is the number of dimensions the line has.
"""
lineData = np.empty((dims, length))
lineData[:, 0] = np.random.rand(dims)
for index in range(1, length):
    # scaling the random numbers by 0.1 so
    # movement is small compared to position.
    # subtraction by 0.5 is to change the range to [-0.5, 0.5]
    # to allow a line to move backwards.
    step = ((np.random.rand(dims) - 0.5) * 0.1)
    lineData[:, index] = lineData[:, index - 1] + step

return lineData


def update_lines(num, dataLines, lines):
    for line, data in zip(lines, dataLines):
    # NOTE: there is no .set_data() for 3 dim data...
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2, :num])
    return lines

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Four lines of random 3-D lines
data = [Gen_RandLine(25, 3) for index in range(4)]

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for 
dat in data]

# Setting the axes properties
ax.set_xlim3d([0.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 1.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs= 
   (data, lines),
                               interval=50, blit=False)

plt.show()

''''

What this does is plots a series of lines in 3D using randomly generated points.

I've reduced the number of plotted animated lines to 4 and I'd like to like to put a label (A,B,C,D) onto the origin point of the lines. I have looked in the API but can only see how to label the axes and ticks, not the plotted data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2025-02-16 16:01:10

我认为我们可以使用注释函数将字符串添加到初始图中,并使动画起作用。因此,设置图形后,我使用动画的初始数据添加文本。

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Four lines of random 3-D lines
data = [Gen_RandLine(25, 3) for index in range(4)]

# add text
name = ['A','B','C','D']
colors = ['blue','orange','green','red']
for i,d in enumerate(data):
    #print('idx:'+str(i), d[0][0], d[1][0], d[2][0])
    ax.text(d[0][0],d[1][0],d[2][0], s=name[i], size=18, color=colors[i])
            
# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]

# Setting the axes properties
ax.set_xlim3d([0.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 1.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines), interval=50, blit=False)

plt.show()

I think we can add a string to the initial graph with the annotation function and make the animation work. So after setting up the graph, I add the text using the initial data for the animation.

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Four lines of random 3-D lines
data = [Gen_RandLine(25, 3) for index in range(4)]

# add text
name = ['A','B','C','D']
colors = ['blue','orange','green','red']
for i,d in enumerate(data):
    #print('idx:'+str(i), d[0][0], d[1][0], d[2][0])
    ax.text(d[0][0],d[1][0],d[2][0], s=name[i], size=18, color=colors[i])
            
# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]

# Setting the axes properties
ax.set_xlim3d([0.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 1.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines), interval=50, blit=False)

plt.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文