【问题标题】:generating multiple graphs on Web Browser using Matplotlib and mpld3使用 Matplotlib 和 mpld3 在 Web 浏览器上生成多个图形
【发布时间】:2018-05-02 09:12:16
【问题描述】:

我正在绘制两个图表。我正在尝试使用 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() 函数可以很好地生成图形,我正在使用 mpld3 库在我的网络浏览器上显示它,当我执行代码时,只有第一个图形正在浏览器上显示而不是第二个,我想要的是两个图表并排显示或一个在另一个下方。我认为现在一个两个图表一个接一个地生成。
  • 欢迎您执行代码,只需一秒钟即可理解我的意思,如果您使用 plt.show() 从我的 IDE 生成两个图,但是使用时mpld3 在我的浏览器上显示它只有第一个每周步行英里数的图表正在显示

标签: python csv matplotlib mpld3


【解决方案1】:

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)

【讨论】:

  • 工作完美,谢谢。 :) 这正是我想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多