如何让我的交互式 Holoviews 图表显示在 Visual Studio 中(没有 Jupyter)?

How do i get my interactive Holoviews graph to display in Visual Studio (without Jupyter)?

在使用 Holoviews 进行交互式绘图时,我主要使用 Jupyter Notebook / Lab。
如何让 Visual Studio 显示我的交互式图表和面板,而不使用 Visual Studio 中的交互式 Jupyter?

在 Visual Studio 中使用来自 Holoviews 等的交互式图表的一种方法是执行代码以在浏览器中显示图表(Holoviews 就是为此而设计的)。
下面的示例将您的 Holoviews 图表放在面板中并启动 Bokeh 服务器。
它会在您的浏览器上打开一个新选项卡并显示您的图表。

# library imports
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)
import panel as pn

# create sample data
data = np.random.normal(size=[50, 2])
df = pd.DataFrame(data, columns=['col1', 'col2'])

# create holoviews graph
hv_plot = hv.Points(df)

# display graph in browser
# a bokeh server is automatically started
bokeh_server = pn.Row(hv_plot).show(port=12345)

# stop the bokeh server (when needed)
bokeh_server.stop()

最简单的解决方案 是将散景设置为渲染器的后端,然后使用bokeh.render.show()。这将在浏览器中打开您的全息图:

hv.extension('bokeh')
from bokeh.plotting import show

show(hv.render(your_holoviews_plot))

完整的工作示例:

# import libraries
import numpy as np
import pandas as pd
import hvplot.pandas
import holoviews as hv

# setting bokeh as backend
hv.extension('bokeh')

# going to use show() to open plot in browser
from bokeh.plotting import show

# create some sample data
data = np.random.normal(size=[50, 2])
df = pd.DataFrame(
    data=data,
    columns=['col1', 'col2'],
)

# using hvplot here to create a holoviews plot
# could have also just used holoviews itself
plot = df.hvplot(kind='scatter', x='col1', y='col2')

# use show() from bokeh
show(hv.render(plot))