matplotlib 畫動態(tài)圖以及plt.ion()和plt.ioff()的使用詳解
學(xué)習(xí)python的道路是漫長的,今天又遇到一個問題,所以想寫下來自己的理解方便以后查看。
在使用matplotlib的過程中,常常會需要畫很多圖,但是好像并不能同時展示許多圖。這是因為python可視化庫matplotlib的顯示模式默認(rèn)為阻塞(block)模式。什么是阻塞模式那?我的理解就是在plt.show()之后,程序會暫停到那兒,并不會繼續(xù)執(zhí)行下去。如果需要繼續(xù)執(zhí)行程序,就要關(guān)閉圖片。那如何展示動態(tài)圖或多個窗口呢?這就要使用plt.ion()這個函數(shù),使matplotlib的顯示模式轉(zhuǎn)換為交互(interactive)模式。即使在腳本中遇到plt.show(),代碼還是會繼續(xù)執(zhí)行。下面這段代碼是展示兩個不同的窗口:
import matplotlib.pyplot as plt plt.ion() # 打開交互模式 # 同時打開兩個窗口顯示圖片 plt.figure() #圖片一 plt.imshow(i1) plt.figure() #圖片二 plt.imshow(i2) # 顯示前關(guān)掉交互模式 plt.ioff() plt.show()
在plt.show()之前一定不要忘了加plt.ioff(),如果不加,界面會一閃而過,并不會停留。那么動態(tài)圖像是如何畫出來的,請看下面這段代碼,具體的解釋就不在這里闡述了,以后有時間再更新:
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add_layer(inputs,in_size,out_size,activation_funiction=None): Weights = tf.Variable(tf.random_normal([in_size,out_size])) biases = tf.Variable(tf.zeros([1,out_size]) +0.1) Wx_plus_b = tf.matmul(inputs,Weights)+biases if activation_funiction is None: outputs = Wx_plus_b else: outputs = activation_funiction(Wx_plus_b) return outputs x_data = np.linspace(-1,1,300)[:,np.newaxis] noise = np.random.normal(0,0.05,x_data.shape) y_data = np.square(x_data)-0.5 +noise xs = tf.placeholder(tf.float32,[None,1]) ys = tf.placeholder(tf.float32,[None,1]) #add hidden layer l1 = add_layer(xs,1,10,activation_funiction=tf.nn.relu) #add output layer prediction = add_layer(l1,10,1,activation_funiction=None) #the error between prediction and real data loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) init =tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(x_data,y_data) plt.ion() #將畫圖模式改為交互模式 for i in range(1000): sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) if i%50 ==0: plt.pause(0.1) try: ax.lines.remove(lines[0]) except Exception: pass prediction_value = sess.run(prediction,feed_dict={xs:x_data}) lines = ax.plot(x_data,prediction_value,'r-',lw=5) print(sess.run(loss,feed_dict={xs:x_data,ys:y_data})) plt.ioff() plt.show()
上面這段代碼執(zhí)行之后就會看到一條曲線在動態(tài)的擬合數(shù)據(jù),直到訓(xùn)練結(jié)束。
下面就來講講matplotlib這兩種模式具體的區(qū)別
在交互模式下:
1、plt.plot(x)或plt.imshow(x)是直接出圖像,不需要plt.show()
2、如果在腳本中使用ion()命令開啟了交互模式,沒有使用ioff()關(guān)閉的話,則圖像會一閃而過,并不會常留。要想防止這種情況,需要在plt.show()之前加上ioff()命令。
在阻塞模式下:
1、打開一個窗口以后必須關(guān)掉才能打開下一個新的窗口。這種情況下,默認(rèn)是不能像Matlab一樣同時開很多窗口進(jìn)行對比的。
2、plt.plot(x)或plt.imshow(x)是直接出圖像,需要plt.show()后才能顯示圖像
到此這篇關(guān)于matplotlib 畫動態(tài)圖以及plt.ion()和plt.ioff()的使用詳解的文章就介紹到這了,更多相關(guān)matplotlib plt.ion() plt.ioff()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch中實現(xiàn)只導(dǎo)入部分模型參數(shù)的方式
今天小編就為大家分享一篇Pytorch中實現(xiàn)只導(dǎo)入部分模型參數(shù)的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Tensorflow訓(xùn)練MNIST手寫數(shù)字識別模型
這篇文章主要為大家詳細(xì)介紹了Tensorflow訓(xùn)練MNIST手寫數(shù)字識別模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02Python 實戰(zhàn)開發(fā)校園管理系統(tǒng)詳細(xì)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過程中查缺補(bǔ)漏,提升水平2021-10-10