如何让HoloViews图或Hvplot在数据库上工作?
生成的图也应该保持所有的交互性。
发布于 2020-05-11 07:07:14
Bokeh file_html
将返回HTML+JS代码,以提供数据的交互式绘图。HTML总大小必须小于20MB。您的数据需要内联到您的HTML,否则您可能会遇到CSRF错误。确保您已经安装了python 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()
displayHoloviews(hvplot.hvPlot(df).line(y=['A','B','C','D']))
发布于 2020-04-24 02:47:20
您可以使用将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
发布于 2021-04-20 18:29:20
您可以简单地使用
import holoviews as hv
your_plot = hv.Points(np.random.multivariate_normal((0,0), [[0.1, 0.1], [0.1, 1.0]], (1000,)))
renderer = hv.renderer("bokeh")
displayHTML(renderer.html(your_plot))
但是,使用displayHTML()
的AFAIU会阻止decimate()
或datashader
的scalability features,它将只显示绘图的初始渲染版本。其他答案也是如此,我不确定当前是否存在在DataBricks上保持可伸缩性的解决方案(我对这样的解决方案 非常感兴趣!)。
https://stackoverflow.com/questions/61394775
复制相似问题