使用matplotlib擦除并重新创建(如果可能的话)子图内擦除和重新创建子图的有效方法?
下面的代码从x
创建一个散点图,并基于w,b
的值,在X上创建线路。
我尝试了几个组合,例如:
fig.canvas.draw()
fig.canvas.flush_events()
plt.clf
plt.cla
但是它们要么 尝试似乎在图上绘制多条线或删除图形 /轴。
可以仅绘制一次散点图,但是线路继续基于w,b
?。
以下是我使用的代码:
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
import time
from IPython.display import display, clear_output
def get_hyperplane_value(x, w, b, offset):
'''
Generate Hyperplane for the plot
'''
return (-w[0] * x + b + offset) / w[1]
def plot_now(ax, W,b):
'''
Visualise the results
'''
x0_1 = np.amin(X[:, 0])
x0_2 = np.amax(X[:, 0])
x1_1 = get_hyperplane_value(x0_1, W, b, 0)
x1_2 = get_hyperplane_value(x0_2, W, b, 0)
x1_1_m = get_hyperplane_value(x0_1, W, b, -1)
x1_2_m = get_hyperplane_value(x0_2, W, b, -1)
x1_1_p = get_hyperplane_value(x0_1, W, b, 1)
x1_2_p = get_hyperplane_value(x0_2, W, b, 1)
ax.plot([x0_1, x0_2], [x1_1, x1_2], "y--")
ax.plot([x0_1, x0_2], [x1_1_m, x1_2_m], "k")
ax.plot([x0_1, x0_2], [x1_1_p, x1_2_p], "k")
x1_min = np.amin(X[:, 1])
x1_max = np.amax(X[:, 1])
ax.set_ylim([x1_min - 3, x1_max + 3])
ax.scatter(X[:, 0], X[:, 1], marker="o", c = y)
return ax
X, y = datasets.make_blobs(n_samples=50, n_features=2, centers=2, cluster_std=1.05, random_state=40)
y = np.where(y == 0, -1, 1)
fig = plt.figure(figsize = (7,7))
ax = fig.add_subplot(1, 1, 1)
for i in range(50):
W = np.random.randn(2)
b = np.random.randn()
ax.cla()
ax = plot_now(ax, W, b)
display(fig)
clear_output(wait = True)
plt.pause(0.25)
The code below creates a Scatter plot from X
and based on values of w,b
, creates lines over X.
I have tried a couple of combinations such as:
fig.canvas.draw()
fig.canvas.flush_events()
plt.clf
plt.cla
But they either seem to plot multiple lines over the plot or Delete the figure / axes.
Is it possible to plot the Scatter plot only once but the Lines keep changing based on w,b
?.
Below is the code that I have used:
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
import time
from IPython.display import display, clear_output
def get_hyperplane_value(x, w, b, offset):
'''
Generate Hyperplane for the plot
'''
return (-w[0] * x + b + offset) / w[1]
def plot_now(ax, W,b):
'''
Visualise the results
'''
x0_1 = np.amin(X[:, 0])
x0_2 = np.amax(X[:, 0])
x1_1 = get_hyperplane_value(x0_1, W, b, 0)
x1_2 = get_hyperplane_value(x0_2, W, b, 0)
x1_1_m = get_hyperplane_value(x0_1, W, b, -1)
x1_2_m = get_hyperplane_value(x0_2, W, b, -1)
x1_1_p = get_hyperplane_value(x0_1, W, b, 1)
x1_2_p = get_hyperplane_value(x0_2, W, b, 1)
ax.plot([x0_1, x0_2], [x1_1, x1_2], "y--")
ax.plot([x0_1, x0_2], [x1_1_m, x1_2_m], "k")
ax.plot([x0_1, x0_2], [x1_1_p, x1_2_p], "k")
x1_min = np.amin(X[:, 1])
x1_max = np.amax(X[:, 1])
ax.set_ylim([x1_min - 3, x1_max + 3])
ax.scatter(X[:, 0], X[:, 1], marker="o", c = y)
return ax
X, y = datasets.make_blobs(n_samples=50, n_features=2, centers=2, cluster_std=1.05, random_state=40)
y = np.where(y == 0, -1, 1)
fig = plt.figure(figsize = (7,7))
ax = fig.add_subplot(1, 1, 1)
for i in range(50):
W = np.random.randn(2)
b = np.random.randn()
ax.cla()
ax = plot_now(ax, W, b)
display(fig)
clear_output(wait = True)
plt.pause(0.25)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您正在尝试为数字动画,因此您应该使用
funcanimation
。动画的基本原理是您初始化行,然后更新值。It appears to me that you are trying to animate a figure, so you should use
FuncAnimation
. The basic principle with animations is that you initialize your lines, and later update the values.