最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

matplotlib 畫(huà)動(dòng)態(tài)圖以及plt.ion()和plt.ioff()的使用詳解

 更新時(shí)間:2021年01月05日 16:17:53   作者:zbrwhut  
這篇文章主要介紹了matplotlib 畫(huà)動(dòng)態(tài)圖以及plt.ion()和plt.ioff()的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

學(xué)習(xí)python的道路是漫長(zhǎng)的,今天又遇到一個(gè)問(wèn)題,所以想寫(xiě)下來(lái)自己的理解方便以后查看。

在使用matplotlib的過(guò)程中,常常會(huì)需要畫(huà)很多圖,但是好像并不能同時(shí)展示許多圖。這是因?yàn)閜ython可視化庫(kù)matplotlib的顯示模式默認(rèn)為阻塞(block)模式。什么是阻塞模式那?我的理解就是在plt.show()之后,程序會(huì)暫停到那兒,并不會(huì)繼續(xù)執(zhí)行下去。如果需要繼續(xù)執(zhí)行程序,就要關(guān)閉圖片。那如何展示動(dòng)態(tài)圖或多個(gè)窗口呢?這就要使用plt.ion()這個(gè)函數(shù),使matplotlib的顯示模式轉(zhuǎn)換為交互(interactive)模式。即使在腳本中遇到plt.show(),代碼還是會(huì)繼續(xù)執(zhí)行。下面這段代碼是展示兩個(gè)不同的窗口:

  import matplotlib.pyplot as plt
  plt.ion()  # 打開(kāi)交互模式
  # 同時(shí)打開(kāi)兩個(gè)窗口顯示圖片
  plt.figure() #圖片一
  plt.imshow(i1)
  plt.figure()  #圖片二
  plt.imshow(i2)
  # 顯示前關(guān)掉交互模式
  plt.ioff()
  plt.show()

在plt.show()之前一定不要忘了加plt.ioff(),如果不加,界面會(huì)一閃而過(guò),并不會(huì)停留。那么動(dòng)態(tài)圖像是如何畫(huà)出來(lái)的,請(qǐng)看下面這段代碼,具體的解釋就不在這里闡述了,以后有時(shí)間再更新:

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()  #將畫(huà)圖模式改為交互模式
 
  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í)行之后就會(huì)看到一條曲線(xiàn)在動(dòng)態(tài)的擬合數(shù)據(jù),直到訓(xùn)練結(jié)束。

下面就來(lái)講講matplotlib這兩種模式具體的區(qū)別

在交互模式下:

1、plt.plot(x)或plt.imshow(x)是直接出圖像,不需要plt.show()

2、如果在腳本中使用ion()命令開(kāi)啟了交互模式,沒(méi)有使用ioff()關(guān)閉的話(huà),則圖像會(huì)一閃而過(guò),并不會(huì)常留。要想防止這種情況,需要在plt.show()之前加上ioff()命令。

在阻塞模式下:

1、打開(kāi)一個(gè)窗口以后必須關(guān)掉才能打開(kāi)下一個(gè)新的窗口。這種情況下,默認(rèn)是不能像Matlab一樣同時(shí)開(kāi)很多窗口進(jìn)行對(duì)比的。

2、plt.plot(x)或plt.imshow(x)是直接出圖像,需要plt.show()后才能顯示圖像

到此這篇關(guān)于matplotlib 畫(huà)動(dòng)態(tài)圖以及plt.ion()和plt.ioff()的使用詳解的文章就介紹到這了,更多相關(guān)matplotlib  plt.ion() plt.ioff()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

元谋县| 孝义市| 莱州市| 临洮县| 东乡| 固阳县| 克拉玛依市| 托克逊县| 哈密市| 简阳市| 黎城县| 托克逊县| 蓬溪县| 天峻县| 舟曲县| 临澧县| 伊吾县| 宜黄县| 汉阴县| 新闻| 玉门市| 华安县| 桓台县| 桐梓县| 耒阳市| 舟山市| 吉林市| 蒲城县| 萍乡市| 长兴县| 泽普县| 拜泉县| 南投县| 纳雍县| 丰顺县| 宾阳县| 濮阳市| 凤山市| 碌曲县| 隆尧县| 揭东县|