gpt4 book ai didi

python - matplotlib - 如何在添加新数据时保持坐标轴不变?

转载 作者:太空狗 更新时间:2023-10-30 02:32:39 33 4
gpt4 key购买 nike

我正在使用 matplotlib 来显示不断更新的数据(大约每秒更改 10 次)。我正在使用 3D 散点图,我希望轴固定在特定范围内,因为数据相对于散点图边缘的位置很重要。

目前每当我添加新数据时,轴将重置为按数据缩放,而不是我想要的大小(当我 hold=False 时)。如果我设置 hold=True,轴将保持正确的大小,但新数据将覆盖在旧数据上,这不是我想要的。

如果我每次获取新数据时都重新缩放轴,我可以让它工作,但这似乎是一种低效的方法,特别是因为我还需要再次进行所有其他格式设置(添加标题、图例等) )

有没有什么方法可以让我只指定一次绘图的属性,并且在我添加新数据时它会保持不变?

这是我的代码的粗略大纲,以帮助解释我的意思:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X_MAX = 50
Y_MAX = 50
Z_MAX = 50

fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.set_title("My Title")
ax.set_xlim3d([0, X_MAX])
ax.set_ylim3d([0, Y_MAX])
ax.set_zlim3d([0, Z_MAX])
ax.set_autoscale_on(False)
# This is so the new data replaces the old data
# seems to be replacing the axis ranges as well, maybe a different method should be used?
ax.hold(False)

plt.ion()
plt.show()

a = 0
while a < 50:
a += 1
ax.scatter( a, a/2+1, 3, s=1 )
# If I don't set the title and axes ranges again here, they will be reset each time
# I want to know if there is a way to only set them once and have it persistent
ax.set_title("My Title")
ax.set_xlim3d([0, X_MAX])
ax.set_ylim3d([0, Y_MAX])
ax.set_zlim3d([0, Z_MAX])
plt.pause(0.001)

编辑:1.我也试过ax.set_autoscale_on(False),但是没有成功2.我用普通的二维散点图试过了,同样的问题仍然存在3.找到一个related question仍然没有答案

最佳答案

我会做这样的事情(注意删除 hold(False) ):

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X_MAX = 50
Y_MAX = 50
Z_MAX = 50
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.set_title("My Title")
ax.set_xlim3d([0, X_MAX])
ax.set_ylim3d([0, Y_MAX])
ax.set_zlim3d([0, Z_MAX])
ax.set_autoscale_on(False)
plt.ion()
plt.show()

a = 0
sct = None
while a < 50:
a += 1
if sct is not None:
sct.remove()
sct = ax.scatter( a, a/2+1, 3, s=1 )
fig.canvas.draw()
plt.pause(0.001)

每次在循环中删除只是添加的散点图。

关于python - matplotlib - 如何在添加新数据时保持坐标轴不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17031660/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com