将ipython Notebook与%matplotlib内联一起使用时,如何保持当前数字? [英] How to keep the current figure when using ipython notebook with %matplotlib inline?

查看:97
本文介绍了将ipython Notebook与%matplotlib内联一起使用时,如何保持当前数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到答案,所以请让我在这里提问.

I could not find answer for this, so please let me ask here.

我想在使用%matplotlib inline时将当前图形保存在ipython笔记本中.有可能吗?

I would like to keep the current figure in ipython notebook when using %matplotlib inline. Is that possible?

例如,我想在图形中绘制2条线

For example, I want to plot 2 lines in a graph

plt.plot([1,2,3,6],[4,2,3,4])

plt.plot([3.3, 4.4, 4.5, 6.5], [3., 5., 6., 7.])

如果我将这两个命令行放在一个单元格中就可以了.该图显示了两条线.但是,如果我将它们分别放入两个单元格,则在执行第二个单元格(plt.plot([3.3,4.4,4.5,6.5],[3.,5.,6.,7.]))时,前一个plot(plt.plot([1,2,3,6],[4,2,3,4]))已清除.我想用第一个单元格的线绘制一个图,用第二个单元格的线绘制一个图.

If I put those two command lines in a cell it is ok. The graph shows two lines. However, if I put them separately into two cells, when the second cell (plt.plot([3.3, 4.4, 4.5, 6.5], [3., 5., 6., 7.])) is executed, the previous plot(plt.plot([1,2,3,6],[4,2,3,4])) is cleared. I want to plot a graph with the line for the first cell and a graph with the two lines for the second cell.

我查找了网站 它通过

plt.cla()  # clear existing plot

但是有点混乱,因为它还是会自动清除.

but it is bit confusing, since it automatically clears anyway.

是否有任何命令无法清除(或保持)Matlab中的保持"等先前图形?

Is there any command to not clear (or keep) the previous plot like "hold on" in Matlab?

推荐答案

使用ax.plot而不是plt.plot来确保两次都绘制到相同的轴上.使用fig(在第二个单元格中)显示绘图.

Use ax.plot instead of plt.plot to make sure you are plotting to the same axes both times. Use fig (in the second cell) to display the plot.

在单元格1中:

%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,6],[4,2,3,4])

在单元格2中:

ax.plot([3.3, 4.4, 4.5, 6.5], [3., 5., 6., 7.])
fig 

收益

这篇关于将ipython Notebook与%matplotlib内联一起使用时,如何保持当前数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆