Python matplotlib實時畫圖案例
實時畫圖
import matplotlib.pyplot as plt ax = [] # 定義一個 x 軸的空列表用來接收動態(tài)的數(shù)據(jù) ay = [] # 定義一個 y 軸的空列表用來接收動態(tài)的數(shù)據(jù) plt.ion() # 開啟一個畫圖的窗口 for i in range(100): # 遍歷0-99的值 ax.append(i) # 添加 i 到 x 軸的數(shù)據(jù)中 ay.append(i**2) # 添加 i 的平方到 y 軸的數(shù)據(jù)中 plt.clf() # 清除之前畫的圖 plt.plot(ax,ay) # 畫出當(dāng)前 ax 列表和 ay 列表中的值的圖形 plt.pause(0.1) # 暫停一秒 plt.ioff() # 關(guān)閉畫圖的窗口
實時畫圖 效果圖
補充知識:Python 繪圖與可視化 matplotlib 動態(tài)條形圖 bar
第一種辦法
一種方法是每次都重新畫,包括清除figure
def animate(fi): bars=[] if len(frames)>fi: # axs.text(0.1,0.90,time_template%(time.time()-start_time),transform=axs.transAxes)#所以這樣 time_text.set_text(time_template%(0.1*fi))#這個必須沒有axs.cla()才行 # axs.cla() axs.set_title('bubble_sort_visualization') axs.set_xticks([]) axs.set_yticks([]) bars=axs.bar(list(range(Data.data_count)),#個數(shù) [d.value for d in frames[fi]],#數(shù)據(jù) 1, #寬度 color=[d.color for d in frames[fi]]#顏色 ).get_children() return bars anim=animation.FuncAnimation(fig,animate,frames=len(frames), interval=frame_interval,repeat=False)
這樣效率很低,而且也有一些不可取的弊端,比如每次都需要重新設(shè)置xticks、假如figure上添加的有其他東西,這些東西也一并被clear了,還需要重新添加,比如text,或者labale。
第二種辦法
可以像平時畫線更新data那樣來更新bar的高
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' import matplotlib.pyplot as plt import numpy as np from matplotlib import animation fig=plt.figure(1,figsize=(4,3)) ax=fig.add_subplot(111) ax.set_title('bar_animate_test') #ax.set_xticks([])注釋了這個是能看到變化,要不看不到變化,不對,能看到變化,去了注釋吧 #ax.set_yticks([]) ax.set_xlabel('xlable') N=5 frames=50 x=np.arange(1,N+1) collection=[] collection.append([i for i in x]) for i in range(frames): collection.append([ci+1 for ci in collection[i]]) print(collection) xstd=[0,1,2,3,4] bars=ax.bar(x,collection[0],0.30) def animate(fi): # collection=[i+1 for i in x] ax.set_ylim(0,max(collection[fi])+3)#對于問題3,添加了這個 for rect ,yi in zip(bars,collection[fi]): rect.set_height(yi) # bars.set_height(collection) return bars anim=animation.FuncAnimation(fig,animate,frames=frames,interval=10,repeat=False) plt.show()
問題
*)TypeError: ‘numpy.int32' object is not iterable
x=np.arange(1,N+1)<br>collection=[i for i in x] #collection=[i for i in list(x)]#錯誤的認為是dtype的原因,將這里改成了list(x) for i in range(frames): collection.append([ci+1 for ci in collection[i]])#問題的原因是因為此時的collection還是一個一位數(shù)組,所以這個collection[i]是一個x里的一個數(shù),并不是一個列表,我竟然還以為的dtype的原因,又改了 xstd=[0,1,2,3,4]
應(yīng)該是
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' collection=[] collection.append([i for i in x])#成為二維數(shù)組 for i in range(frames): collection.append([ci+1 for ci in collection[i]])
然后又出現(xiàn)了下面的問題:
*)TypeError: only size-1 arrays can be converted to Python scalars
Traceback (most recent call last): File "forTest.py", line 22, in <module> bars=ax.bar(x,collection,0.30) File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\__init__.py", line 1589, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar label='_nolegend_', File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 707, in __init__ Patch.__init__(self, **kwargs) File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 89, in __init__ self.set_linewidth(linewidth) File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 368, in set_linewidth self._linewidth = float(w) TypeError: only size-1 arrays can be converted to Python scalars
應(yīng)該是傳遞的參數(shù)錯誤,仔細想了一下,在報錯的代碼行中,collection原來是沒錯的,因為原來是一維數(shù)組,現(xiàn)在變成二維了,改為
bars=ax.bar(x,collection[0],0.30)
好了
*)出現(xiàn)的問題,在上面的代碼中,運行的時候不會畫布的大小不會變,會又條形圖溢出的情況,在animate()中添加了
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學(xué)習(xí)交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學(xué)習(xí)教程和PDF電子書! ''' def animate(fi): # collection=[i+1 for i in x] ax.set_ylim(0,max(collection[fi])+3)#添加了這個 for rect ,yi in zip(bars,collection[fi]): rect.set_height(yi) # bars.set_height(collection) return bars
別的屬性
*)條形圖是怎樣控制間隔的:
是通過控制寬度
width=1,#沒有間隔,每個條形圖會緊挨著
*)errorbar:
是加一個橫線,能通過xerr和yerr來調(diào)整方向
xstd=[0,1,2,3,4]
bars=ax.bar(x,collection,0.30,xerr=xstd)
以上這篇Python matplotlib實時畫圖案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python django 增刪改查操作 數(shù)據(jù)庫Mysql
下面小編就為大家?guī)硪黄猵ython django 增刪改查操作 數(shù)據(jù)庫Mysql。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Python基礎(chǔ)之logging模塊知識總結(jié)
用Python寫代碼的時候,在想看的地方寫個print xx 就能在控制臺上顯示打印信息,這樣子就能知道它是什么了,但是當(dāng)我需要看大量的地方或者在一個文件中查看的時候,這時候print就不大方便了,所以Python引入了logging模塊來記錄我想要的信息,需要的朋友可以參考下2021-05-05