在 python Panel 中,如何在面板中显示 matplotlib 图。选项卡视图?

In python Panel, how to show a matplotlib plot in a panel.Tabs view?

提问人:Xtiaan 提问时间:6/27/2023 最后编辑:Davide_sdXtiaan 更新时间:6/28/2023 访问量:154

问:

Python Panel 库建议使用 Bokeh 进行绘图,但 Bokeh 包中缺少一些统计数字。我想使用 matplotlib(或者可能是 seaborn)来创建可以放置在面板中的图形。制表符。

例如,以下代码有效,但使用散景而不是 matplotlib:

import panel as pn
from bokeh.plotting import figure

p1 = figure(width=400, height=400, name="Line 1")
p1.line([1, 2, 3], [1, 2, 3])

p2 = figure(width=400, height=400, name='Line 2')
p2.line([0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 2, 1, 0])

pn.Tabs(p1, p2)

这(正确地)显示:

enter image description here

但是,我每次尝试显示 matplotlib 中的图像都失败了。类似的东西根本行不通。p1 = plt.plot(df['wage'])

python matplotlib jupyter-notebook holoviz holoviz-panel

评论


答:

2赞 Davide_sd 6/28/2023 #1

Holoviz Panel 使用窗格的概念来渲染图形(绘图图形、散景图形、matplotlib 图形)......例如,要渲染一个 matplotlib 图形,您必须使用(这里是它的文档)。pn.pane.Matplotlib

请注意,它需要创建图形,而不是更传统的 .matplotlib.figure.Figureplt.figure

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import panel as pn
pn.extension()

fig1 = Figure()
ax1 = fig1.subplots()
ax1.plot([1, 2, 3], [1, 2, 3])

fig2 = Figure()
ax2 = fig2.subplots()
ax2.plot([0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 2, 1, 0])

pane1 = pn.pane.Matplotlib(fig1, dpi=96)
pane2 = pn.pane.Matplotlib(fig2, dpi=96)
pn.Tabs(("title 1", pane1), ("title 2", pane2))