如何让HoloViews图或Hvplot在数据库上工作?
生成的图也应该保持所有的交互性。
上云精选
2核2G云服务器 每月9.33元起,个人开发者专属3年机 低至2.3折
您可以使用 将HoloViews绘图另存为displayHTML文件,然后使用displayHTML() 。
解决方案的灵感来自于这个博客:
https://anitagraser.com/2020/02/02/first-working-movingpandas-setup-on-databricks/
下面是一个 工作示例
# import libraries import numpy as np import pandas as pd import holoviews as hv import hvplot.pandas # create sample date df = pd.DataFrame(np.random.rand(50, 2), columns=['col1', 'col2']) df['col3'] = np.random.randint(0, 2, 50) # create holoviews scatter plot hv_scatter = df.hvplot(kind='scatter', x='col1', y='col2', groupby='col3') # save scatter plot as html hv.save(hv_scatter, 'hv_scatter.html') # assign html file to variable with open('hv_scatter.html', 'r') as html_file: html_scatter = html_file.read() # display scatter plot displayHTML(html_scatter)
作为替代方案,您也可以将您的绘图呈现为Bokeh绘图,然后使用此笔记本的示例:
https://docs.databricks.com/notebooks/visualizations/bokeh.html
Bokeh file_html 将返回HTML+JS代码,以提供数据的交互式绘图。HTML总大小必须小于20MB。您的数据需要内联到您的HTML,否则您可能会遇到CSRF错误。确保您已经安装了python holoviews 、 bokeh 和 hvplot 包。示例:
file_html
holoviews
bokeh
hvplot
import math import numpy as np import pandas as pd import holoviews as hv from bokeh.embed import components, file_html from bokeh.resources import CDN hv.extension('bokeh') import hvplot renderer = hv.renderer('bokeh').instance(fig='html', holomap='auto') # see https://github.com/ioam/holoviews/issues/1819 def displayHoloviews(hv_plot, html_name="plot.html", width=1000, height=600, renderer=renderer): plot = renderer.get_plot(hv_plot).state setattr(plot, 'plot_width', width) setattr(plot, 'plot_height', height) displayHTML(file_html(plot, CDN, html_name)) index = pd.date_range('1/1/2000', periods=1000) df = pd.DataFrame(np.random.randn(1000, 4), index=index, columns=list('ABCD')).cumsum()