使用 Matplotlib 和 mpld3 在 Web 浏览器上生成多个图形
generating multiple graphs on Web Browser using Matplotlib and mpld3
我正在绘制两个图表。我正在尝试使用 mpld3 库在我的网络浏览器上绘制多个 matplotlib 图。我在 mpld3.show()
函数的帮助下成功绘制了第一张图,但未加载另一张图。
任何人都可以帮助我了解如何在浏览器上获取这两个图表,我相信只需一行代码即可解决问题。
import matplotlib.pyplot as plt, mpld3
x = [1,2,3]
y = [2,3,4]
#firstgraph
plt.xlabel("xlabel 1")
plt.ylabel("ylabel 1")
plt.title("Plot 1")
plt.legend()
plt.bar(x,y, label = 'label for bar', color = 'b')
mpld3.show()
#secondgraph
x = [1,2,3]
y = [5,3,1]
plt.xlabel("xlabel 2")
plt.ylabel("ylabel 2")
plt.title("Plot 2")
plt.bar(x,y, color = 'r')
mpld3.show()
与 plt.show()
一样,脚本的执行将在将输出提供给浏览器时停止。
你可以按Ctrl+C停止服务器,代码继续第二个数字。第二个数字将显示在新的浏览器选项卡中。
另一方面,您也可以通过单独创建它们的 html 表示并加入要提供的 html 来同时向浏览器提供两个图形。
import matplotlib.pyplot as plt
import mpld3
from mpld3._server import serve
#firstgraph
x = [1,2,3]
y = [2,3,4]
fig1 = plt.figure()
plt.xlabel("xlabel 1")
plt.ylabel("ylabel 1")
plt.title("Plot 1")
plt.legend()
plt.bar(x,y, label = 'label for bar', color = 'b')
#secondgraph
x = [1,2,3]
y = [5,3,1]
fig2 =plt.figure()
plt.xlabel("xlabel 2")
plt.ylabel("ylabel 2")
plt.title("Plot 2")
plt.bar(x,y, color = 'r')
# create html for both graphs
html1 = mpld3.fig_to_html(fig1)
html2 = mpld3.fig_to_html(fig2)
# serve joined html to browser
serve(html1+html2)
我正在绘制两个图表。我正在尝试使用 mpld3 库在我的网络浏览器上绘制多个 matplotlib 图。我在 mpld3.show()
函数的帮助下成功绘制了第一张图,但未加载另一张图。
任何人都可以帮助我了解如何在浏览器上获取这两个图表,我相信只需一行代码即可解决问题。
import matplotlib.pyplot as plt, mpld3
x = [1,2,3]
y = [2,3,4]
#firstgraph
plt.xlabel("xlabel 1")
plt.ylabel("ylabel 1")
plt.title("Plot 1")
plt.legend()
plt.bar(x,y, label = 'label for bar', color = 'b')
mpld3.show()
#secondgraph
x = [1,2,3]
y = [5,3,1]
plt.xlabel("xlabel 2")
plt.ylabel("ylabel 2")
plt.title("Plot 2")
plt.bar(x,y, color = 'r')
mpld3.show()
与 plt.show()
一样,脚本的执行将在将输出提供给浏览器时停止。
你可以按Ctrl+C停止服务器,代码继续第二个数字。第二个数字将显示在新的浏览器选项卡中。
另一方面,您也可以通过单独创建它们的 html 表示并加入要提供的 html 来同时向浏览器提供两个图形。
import matplotlib.pyplot as plt
import mpld3
from mpld3._server import serve
#firstgraph
x = [1,2,3]
y = [2,3,4]
fig1 = plt.figure()
plt.xlabel("xlabel 1")
plt.ylabel("ylabel 1")
plt.title("Plot 1")
plt.legend()
plt.bar(x,y, label = 'label for bar', color = 'b')
#secondgraph
x = [1,2,3]
y = [5,3,1]
fig2 =plt.figure()
plt.xlabel("xlabel 2")
plt.ylabel("ylabel 2")
plt.title("Plot 2")
plt.bar(x,y, color = 'r')
# create html for both graphs
html1 = mpld3.fig_to_html(fig1)
html2 = mpld3.fig_to_html(fig2)
# serve joined html to browser
serve(html1+html2)