[英]Matplotlib not changing the x scale for continuous displaying data
我已經嘗試過類似以下的事情:
import matplotlib.pyplot as plt
import numpy as np
d= [1,2,3,4]
f = [1,2,3,4,5]
plt.ion()
fig = plt.figure()
fig.set_size_inches(10,5)
ax1 = fig.add_subplot(111)
line1, = ax1.plot(np.arange(len(d)),d,color='blue', label='first',marker=".")
line2, = ax1.plot(np.arange(len(f)),f,color='red', label='second',marker=".")
plt.show()
for i in range(20):
d.append(i)
f.append(i)
line1.set_xdata(np.arange(len(d)))
line2.set_xdata(np.arange(len(f)))
line1.set_ydata(d)
line2.set_ydata(f)
得到這樣的錯誤:
>>> Exception in Tkinter callback
Traceback (most recent call last):ges\numpy\lib\stride_tricks.py", line 249, in broadcast_arrays
File "C:\Python35\lib\site-packages\matplotlib\backends\tkagg.py", line 26, in blit
dataptr, colormode, bboxptr)kages\numpy\lib\stride_tricks.py", line 184, in _broadcast_shape
_tkinter.TclError: this isn't a Tk application
ValueError: shape mismatch: objects cannot be broadcast to a single shape
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Python35\lib\tkinter\__init__.py", line 596, in callit
func(*args)
File "C:\Python35\lib\site-packages\matplotlib\backends\_backend_tk.py", line 310, in idle_draw
self.draw()
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 13, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "C:\Python35\lib\site-packages\matplotlib\backends\tkagg.py", line 34, in blit
dataptr, colormode, bboxptr)
_tkinter.TclError: this isn't a Tk application
即使它沒有改變 x 軸值。
我想像實時顯示一樣在同一個圖形中顯示這兩個值。
數組有不同的大小。 從稱為重復的問題中應用解決方案后,我收到錯誤:
ValueError:形狀不匹配:對象不能廣播到單個形狀
我的查詢與不同的數組大小和實時繪圖有關。
我懷疑錯誤來自 for 循環太快,但我無法重現它。 所以讓我們讓動畫正確。 通常您會想要設置延遲以感知繪圖中的變化。 plt.pause()
使用plt.pause()
。
為了將繪圖重新調整為新添加的數據,請使用.relim()
和.autoscale_view()
。
最后,您可能希望在循環終止時保持繪圖打開; 這可以通過關閉交互模式並再次調用show
來完成。
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np
d= [1,2,3,4]
f = [1,2,3,4,5]
plt.ion()
fig = plt.figure()
fig.set_size_inches(10,5)
ax1 = fig.add_subplot(111)
line1, = ax1.plot(np.arange(len(d)),d,color='blue', label='first',marker=".")
line2, = ax1.plot(np.arange(len(f)),f,color='red', label='second',marker=".")
plt.show()
for i in range(20):
d.append(i)
f.append(i)
line1.set_xdata(np.arange(len(d)))
line2.set_xdata(np.arange(len(f)))
line1.set_ydata(d)
line2.set_ydata(f)
ax1.relim()
ax1.autoscale_view()
plt.pause(0.5)
# To keep the plot open after the animation has finished.
plt.ioff()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.