我用for循环绘制了多个地块,但是我得到了一个(不需要的)额外的子图。我如何摆脱它?
我正在编程jupyterlab笔记本中的随机步行模拟器。使用for循环,我希望模拟器生成多个数据集,然后将其绘制在单个图形上。我设法这样做。这是我的代码(包括随机步行模拟器的代码):
#RANDOM WALK SIMULATOR
import matplotlib.pyplot as plt
import numpy as np
def random_walk(random_state_index, initial_position=(0, 0), steps=1000):
np.random.RandomState(random_state_index)
X, Y = [initial_position[0]], [initial_position[0]]
for i in range(steps):
plt.pause(0.00001)
# Random step choice
ways = ["up", "down", "left", "right"]
direction = np.random.choice(ways, p = [0.3,0.2,0.25,0.25])
if direction == "up":
X.append(X[-1])
Y.append(Y[-1] + 0.1)
if direction == "down":
X.append(X[-1])
Y.append(Y[-1] - 0.1)
if direction == "left":
X.append(X[-1] - 0.1)
Y.append(Y[-1])
if direction == "right":
X.append(X[-1] + 0.1)
Y.append(Y[-1])
return X,Y
#PLOT
fig, ax = plt.subplots()
for i in range(10):
X,Y = random_walk(random_state_index = i)
ax.plot(X,Y)
fig
前面是一个链接,显示我的输出的外观。如您所见,我得到了一个额外的空图。我想摆脱它,但是我很难找出如何: 输出
谢谢!
解决方案: 我找到了一个解决方案,而无需诉诸交互模式。这是解决方案:
%matplotlib inline
plt.show(block = True)
fig, ax = plt.subplots()
for i in range(10):
X,Y = random_walk(random_state_index = i)
ax.plot(X,Y)
I'm programming a random walk simulator in a JupyterLab Notebook. With a for loop, I want the simulator to generate multiple datasets that are then plotted on a single figure. I managed to do this. Here's my code (including the code for the random walk simulator):
#RANDOM WALK SIMULATOR
import matplotlib.pyplot as plt
import numpy as np
def random_walk(random_state_index, initial_position=(0, 0), steps=1000):
np.random.RandomState(random_state_index)
X, Y = [initial_position[0]], [initial_position[0]]
for i in range(steps):
plt.pause(0.00001)
# Random step choice
ways = ["up", "down", "left", "right"]
direction = np.random.choice(ways, p = [0.3,0.2,0.25,0.25])
if direction == "up":
X.append(X[-1])
Y.append(Y[-1] + 0.1)
if direction == "down":
X.append(X[-1])
Y.append(Y[-1] - 0.1)
if direction == "left":
X.append(X[-1] - 0.1)
Y.append(Y[-1])
if direction == "right":
X.append(X[-1] + 0.1)
Y.append(Y[-1])
return X,Y
#PLOT
fig, ax = plt.subplots()
for i in range(10):
X,Y = random_walk(random_state_index = i)
ax.plot(X,Y)
fig
Ahead is a link that shows what my output looks like. As you can see, I get an extra, empty plot. I'd like to get rid of it, but I'm having difficulty finding out how:
OUTPUT
Thank you!
SOLUTION:
I found a solution without resorting to interactive mode. Here's the solution:
%matplotlib inline
plt.show(block = True)
fig, ax = plt.subplots()
for i in range(10):
X,Y = random_walk(random_state_index = i)
ax.plot(X,Y)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到上述图的原因是因为Ipython笔记本在线返回。为防止发生这种情况,请使用特定的绘制调用(不会返回任何内容)
plt.show()
。换句话说,您要两次要求该图。一旦在单元格的底部(编写
fig
),然后默认情况下,使用Inline Matplotlib绘图。The reason you're seeing the above plot is because it is returned inline by the IPython Notebook. To prevent that from happening, use the specific draw call (which doesn't return anything)
plt.show()
.In other words, you're asking for the figure twice. Once at the bottom of the cell (when you write
fig
) and then again, by default, with the inline matplotlib drawing.