为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

江户川乱折腾 2022-01-05 10:35:08
目前,我有 3 个带有 pyplot 的单独窗口......我不想要......我想在一个类似于 jupyter 输出的单个 Web 浏览器窗口中看到我的所有图按顺序“内联”列出,除了我的脚本从作为 ipython 脚本的 Windows 命令行...# File: plotit.ipy# toy ipython plotting exampleimport numpy as npimport matplotlib.pyplot as plt %matplotlibt = 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)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.ipyUsing matplotlib backend: TkAgg    有没有办法将 Jupiter 文档转换为 ipython 脚本,并从命令行将它作为一个整体脚本运行,但仍然让 matplotlib 图在 jupyter 输出窗口中按顺序显示为“内联”脚本。
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

我不知道是否可以模拟 jupyter 笔记本,而无需实际运行它。


另一种方法是使用webagg后端。在这种情况下,只plt.show()在末尾放置一个调用以在同一页面上显示所有三个数字。


import numpy as np

import matplotlib

matplotlib.use("webagg")

import matplotlib.pyplot as plt 



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)


# 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()

运行此程序时,应打开一个浏览器窗口,并显示三个数字

//img1.sycdn.imooc.com//61d503fa000195f310870920.jpg

查看完整回答
反对 回复 2022-01-05
?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

将所有绘图保存为图像文件而不显示它们,然后在运行python脚本后编写一个html文件来显示所有图像文件。


# File: plotit.ipy


# toy ipython plotting example

import numpy as np

import matplotlib.pyplot as plt 


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

fig = plt.figure()

plt.plot(t,y1)

fig.savefig("plot1.png")


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

fig = plt.figure()

plt.plot(t,y2)

fig.savefig("plot2.png")


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

fig = plt.figure()

plt.plot(t,y3)

fig.savefig("plot3.png")

html:


<!-- FILE: results.html -->

<html>

<body>

    <img src="plot1.png"/><br>

    <img src="plot2.png"/><br>

    <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>


查看完整回答
反对 回复 2022-01-05
  • 2 回答
  • 0 关注
  • 322 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号