Num*_*umb 5 python plot axis real-time matplotlib
我想在实时绘图中操纵x轴,这样一次最多可以看到10个样本.在初始化绘图之后,似乎plt.axis()仅更新一次.有什么建议?提前致谢!
import numpy as np
import matplotlib.pyplot as plt
# Initialize
x_axis_start = 0
x_axis_end = 10
plt.axis([x_axis_start, x_axis_end, 0, 1])
plt.ion()
# Realtime plot
for i in range(100):
y = np.random.random()
plt.scatter(i, y)
plt.pause(0.10)
# print(i)
if i%10 == 0 and i>1:
# print("Axis should update now!")
plt.axis([x_axis_start+10, x_axis_end+10, 0, 1])
Run Code Online (Sandbox Code Playgroud)
你必须更新x_axist_start
并x_axis_end
在if
声明中!
if i%10 == 0 and i>1:
print("Axis should update now!")
x_axis_start += 10
x_axis_end += 10
plt.axis([x_axis_start, x_axis_end, 0, 1])
Run Code Online (Sandbox Code Playgroud)
这招就起作用了!:)
解释:您只为这两个参数添加了一次 10。最后你总是把 10 加上 0 和 10,只剩下一次更新。