簡體   English   中英

在 matplotlib.animation 中為 3D 繪圖線添加標簽

[英]Adding labels to 3D plotlines in matplotlib.animation

我正在嘗試使用 matplotlib.animation 為動畫 3D 情節線添加標簽。 我使用的示例代碼基於matplotlib 示例文件中的這個示例編輯:完整代碼在這里:

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,但只能看到如何標記軸和刻度,而不是繪制的數據。

我認為我們可以使用注釋功能向初始圖形添加一個字符串並使動畫工作。 因此,在設置圖表后,我使用動畫的初始數據添加文本。

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM