溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

plt.ion()與plt.ioff()怎么在matplotlib中使用

發(fā)布時(shí)間:2021-01-06 15:31:19 來源:億速云 閱讀:496 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)plt.ion()與plt.ioff()怎么在matplotlib中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

在plt.show()之前一定不要忘了加plt.ioff(),如果不加,界面會(huì)一閃而過,并不會(huì)停留。那么動(dòng)態(tài)圖像是如何畫出來的,請看下面這段代碼,具體的解釋就不在這里闡述了,以后有時(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()  #將畫圖模式改為交互模式
 
  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ì)看到一條曲線在動(dòng)態(tài)的擬合數(shù)據(jù),直到訓(xùn)練結(jié)束。

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

在交互模式下:

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

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

在阻塞模式下:

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

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

看完上述內(nèi)容,你們對plt.ion()與plt.ioff()怎么在matplotlib中使用有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI