matplotlib:从命令行运行 ipython 时,在 Web 浏览器中按顺序显示绘图

目前,我有 3 个带有 pyplot 的单独窗口......我不想要......我想在一个类似于 jupyter 输出的单个 Web 浏览器窗口中看到我的所有图按顺序“内联”列出,除了我的脚本从作为 ipython 脚本的 Windows 命令行...


# File: plotit.ipy


# toy ipython plotting example

import numpy as np

import matplotlib.pyplot as plt 

%matplotlib


t = np.linspace(0, 10, 100)

y1 = 5*t

y2 = -0.5*t

y3 = 2*t**2


# display as 1st inline graph in "jupyter web browser window"

plt.figure()

plt.plot(t,y1)

plt.show()


# display as 2nd inline graph in "jupyter web browser window"

plt.figure()

plt.plot(t,y2)

plt.show()


# display as 3rd inline graph in "jupyter web browser window"

plt.figure()

plt.plot(t,y3, '.')

plt.show()

这是我使用 ActiveState Python 3.6 从命令行运行它的方式:


C:\> ipython 

In [1]: %run testit.ipy

Using matplotlib backend: TkAgg    

有没有办法将 Jupiter 文档转换为 ipython 脚本,并从命令行将它作为一个整体脚本运行,但仍然让 matplotlib 图在 jupyter 输出窗口中按顺序显示为“内联”脚本。


江户川乱折腾
浏览 324回答 2
2回答

慕标5832272

我不知道是否可以模拟 jupyter 笔记本,而无需实际运行它。另一种方法是使用webagg后端。在这种情况下,只plt.show()在末尾放置一个调用以在同一页面上显示所有三个数字。import numpy as npimport matplotlibmatplotlib.use("webagg")import matplotlib.pyplot as plt t = np.linspace(0, 10, 100)y1 = 5*ty2 = -0.5*ty3 = 2*t**2# display as 1st inline graph in "jupyter web browser window"plt.figure()plt.plot(t,y1)# display as 2nd inline graph in "jupyter web browser window"plt.figure()plt.plot(t,y2)# display as 3rd inline graph in "jupyter web browser window"plt.figure()plt.plot(t,y3, '.')plt.show()运行此程序时,应打开一个浏览器窗口,并显示三个数字

天涯尽头无女友

将所有绘图保存为图像文件而不显示它们,然后在运行python脚本后编写一个html文件来显示所有图像文件。# File: plotit.ipy# toy ipython plotting exampleimport numpy as npimport matplotlib.pyplot as plt&nbsp;t = np.linspace(0, 10, 100)y1 = 5*ty2 = -0.5*ty3 = 2*t**2# display as 1st inline graph in jupyter web browser windowfig = plt.figure()plt.plot(t,y1)fig.savefig("plot1.png")# display as 2nd inline graph in jupyter web browser windowfig = plt.figure()plt.plot(t,y2)fig.savefig("plot2.png")# display as 3rd inline graph in jupyter web browser windowfig = plt.figure()plt.plot(t,y3)fig.savefig("plot3.png")html:<!-- FILE: results.html --><html><body>&nbsp; &nbsp; <img src="plot1.png"/><br>&nbsp; &nbsp; <img src="plot2.png"/><br>&nbsp; &nbsp; <img src="plot3.png"/><br></body></html>另一个 html 结果页面:<h1>Results</h1><table><tr><td><img src="plot1.png" style="max-width:100%"/></td><td><img src="plot2.png" style="max-width:100%"/></td></tr><tr><td><img src="plot3.png" style="max-width:100%"/></td><td><img src="plot1.png" style="max-width:100%"/></td></tr></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python