Numpy 通过迭代更新图形,matplotlib

Numpy 通过迭代更新图形,matplotlib,numpy,matplotlib,Numpy,Matplotlib,我试图通过迭代一个接一个地绘制数据集的特征。 因此,我希望图形在循环过程中不断更新 我引用了这个帖子,但是答案到处都是,尽管包含了他们的一些建议,如下所示,我仍然无法让代码正常工作。我用的是Jupyter笔记本 import matplotlib.pyplot as plt %matplotlib inline import numpy as np colors = ["darkblue", "darkgreen"] f, (ax1, ax2) = plt.subplots(1, 2, sha

我试图通过迭代一个接一个地绘制数据集的特征。 因此,我希望图形在循环过程中不断更新

我引用了这个帖子,但是答案到处都是,尽管包含了他们的一些建议,如下所示,我仍然无法让代码正常工作。我用的是Jupyter笔记本

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
colors = ["darkblue", "darkgreen"]

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex = True)


for i in range(X.shape[-1]-1):
    idx = np.where(y == 1)[0]
    ax1.scatter(X[idx, i], X[idx, i+1], color=colors[0], label=1)


    idx = np.where(y == 0)[0]
    ax2.scatter(X[idx, i], X[idx, i+1], color=colors[1], label=0)

    plt.draw()
    plt.pause(0.0001)
有什么建议吗


多谢各位

对于动画,您需要一个交互式后端<代码>%matplotlib inline不是交互式后端(它基本上显示了图的打印版本)

您可能决定不使用jupyter运行代码,而是作为脚本运行。在这种情况下,您需要将
plt.ion()
置于交互模式

另一个选项是使用
函数动画
,例如。要在Jupyter中运行这样一个
函数动画
,您仍然需要一些交互式后端,
%matplotlib tk
%matplotlib notebook

从matplotlib 2.1开始,我们还可以使用JavaScript创建动画

from IPython.display import HTML
HTML(ani.to_jshtml())
一些完整的例子:

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])

def animate(i):
    l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

from IPython.display import HTML
HTML(ani.to_jshtml())

对于动画,您需要一个交互式后端<代码>%matplotlib inline不是交互式后端(它基本上显示了图的打印版本)

您可能决定不使用jupyter运行代码,而是作为脚本运行。在这种情况下,您需要将
plt.ion()
置于交互模式

另一个选项是使用
函数动画
,例如。要在Jupyter中运行这样一个
函数动画
,您仍然需要一些交互式后端,
%matplotlib tk
%matplotlib notebook

从matplotlib 2.1开始,我们还可以使用JavaScript创建动画

from IPython.display import HTML
HTML(ani.to_jshtml())
一些完整的例子:

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])

def animate(i):
    l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

from IPython.display import HTML
HTML(ani.to_jshtml())

这是一个在Jupyter笔记本中实时绘图的示例

%matplotlib inline
%load_ext autoreload  #Reload all modules every time before executing the Python code typed.
%autoreload 2 
%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
import time

colors = ["darkblue", "darkgreen"]

# initialise the graph and settings

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = fig.add_subplot(211)
plt.ion() # interactive mode
fig.show()
fig.canvas.draw() # matplotlib canvas drawing

# plotting loop

for i in range(X.shape[-1]-1):
  ax1.clear()
  ax2.clear()

  idx = np.where(y == 1)[0]
  ax1.scatter(X[idx, i], X[idx, i+1], color=colors[0], label=1)

  idx = np.where(y == 0)[0]
  ax2.scatter(X[idx, i], X[idx, i+1], color=colors[1], label=0)

  fig.canvas.draw() # draw
  time.sleep(0.5)   # sleep

这是一个在Jupyter笔记本中实时绘图的示例

%matplotlib inline
%load_ext autoreload  #Reload all modules every time before executing the Python code typed.
%autoreload 2 
%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
import time

colors = ["darkblue", "darkgreen"]

# initialise the graph and settings

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = fig.add_subplot(211)
plt.ion() # interactive mode
fig.show()
fig.canvas.draw() # matplotlib canvas drawing

# plotting loop

for i in range(X.shape[-1]-1):
  ax1.clear()
  ax2.clear()

  idx = np.where(y == 1)[0]
  ax1.scatter(X[idx, i], X[idx, i+1], color=colors[0], label=1)

  idx = np.where(y == 0)[0]
  ax2.scatter(X[idx, i], X[idx, i+1], color=colors[1], label=0)

  fig.canvas.draw() # draw
  time.sleep(0.5)   # sleep

谢谢,等我有机会的话,我会去测试的@你测试过吗?我认为这不应该工作,只显示动画完成后的最终数字。它似乎是工作。我现在正在测试。我会再检查一下。谢谢。我似乎不知道如何与你的新代码共享轴。我正试图使用
子批恢复到我的旧代码,但它产生了一个错误。谢谢,一旦有机会,我将测试它。=)@你测试过吗?我认为这不应该工作,只显示动画完成后的最终数字。它似乎是工作。我现在正在测试。我会再检查一下。谢谢。我似乎不知道如何与你的新代码共享轴。我正试图用
子批
恢复到我的旧代码,但它产生了一个错误。