亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

運行此程序時,應打開一個瀏覽器窗口,并顯示三個數字

http://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 關注
  • 306 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載 公眾號

掃描二維碼
關注慕課網微信公眾號