[英]matplotlib set_data() not updating plot on next draw()
I have a 2D plot placed on one of the walls of a 3D plot that doesn't seem to reflect any changes from set_data()
, I would like to understand what I'm doing wrong here.我有一个 2D plot 放置在 3D plot 的墙壁之一上,这似乎并没有反映我在这里做错了什么,我想了解
set_data()
的任何变化。
Here is a sample code showing the 3D plot with the 2D 'projection' plot in question.下面是一个示例代码,显示了 3D plot 和二维“投影” plot。 The output is shown here:
output 如下所示:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
# Test data for projection onto xz plane
t = linspace(0,10, num=20)
z = np.sin(t)
# Plot projection
projx, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'r', zdir='y', zs=1)
# Labels and scaling
ax.set_xlabel('M_x')
ax.set_ylabel('M_y')
ax.set_zlabel('M_z')
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
# Update projection data
projx.set_data([0],[0])
# See if actually updated data
print(projx.get_xdata())
# Draw and display window
plt.draw()
ax.legend()
plt.show()
I imagine that this line:我想这条线:
projx.set_data([0],[0])
would make the projection plot virtually empty.将使投影 plot 几乎为空。 Instead, the sine wave remains.
相反,正弦波仍然存在。
Furthermore, the printout yields [0]
as expected, so the set_data()
call was successful, but for some reason the plot doesn't get drawn with the new data.此外,打印输出按预期产生
[0]
,因此set_data()
调用成功,但由于某种原因,plot 未使用新数据绘制。
Shouldn't the set_data() changes be reflected when drawn afterwards?之后绘制时不应该反映 set_data() 更改吗?
There is a way to update a Line3D
object by directly setting its vertices.有一种方法可以通过直接设置其顶点来更新
Line3D
object。 Not sure, if this might have any negative side effects, though.不过,不确定这是否会产生任何负面影响。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Test data for projection onto xz plane
t = np.linspace(0,10, num=20)
z = np.sin(t)
# Plot projections
projx, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'r', zdir='y', zs=1, label="changed")
projy, = ax.plot(np.linspace(-1,0, num=len(z)), z, 'b', zdir='x', zs=-1, label="not changed")
# Labels and scaling
ax.set_xlabel('M_x')
ax.set_ylabel('M_y')
ax.set_zlabel('M_z')
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
#update vertices of one Line3D object
projx._verts3d = [0, 0.2, 0.7], [1, 1, 1], [0.5, 0.2, 0.7]
ax.legend()
plt.show()
However, since one cannot omit any of the x
, y
, and z
arrays, there is no real advantage over plotting it as a 3D array with one array being a constant.但是,由于不能省略
x
、 y
和z
arrays 中的任何一个,因此将其绘制为 3D 数组并没有真正的优势,其中一个数组是常数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.