1

홀로 뷰 (Holoviews)를 처음으로 시험해보고 있는데, 나는이 애니메이션 "Gapminder"플롯을 here과 같이 재현하고 싶습니다.Holoviews 예제 : Jupyter 노트에 플롯을 표시하는 방법은 무엇입니까?

코드는 실행되지만 Jupyter Notebook에 표시되도록 출력을 처리하는 방법을 알지 못합니다. Jupyter가 임의 HTML을 표시 할 수 있기 때문에 가능하다고 가정합니다. 특히

# Get HoloViews plot and attach document 
doc = curdoc() 
hvplot = BokehRenderer.get_plot(hvgapminder, doc) 

# Make a bokeh layout and add it as the Document root 
plot = layout([[hvplot.state], [slider, button]], sizing_mode='fixed') 
doc.add_root(plot) 

, 나는 결과 doc 또는 hvplot 객체 어떻게해야합니까?

답변

2

이 특별한 예는 HoloViews와 bokeh 구성 요소를 결합한 것으로 bokeh 위젯은 노트북에서 Python과 쉽게 통신 할 수 없습니다. 그러나 holoviews '스크러버'위젯을 사용하여 동일한 것을 달성 할 수 있습니다.

import pandas as pd 
import numpy as np 
import holoviews as hv 
from bokeh.sampledata import gapminder 

hv.extension('bokeh') 

# Switch to sending data 'live' and using the scrubber widget 
%output widgets='live' holomap='scrubber' 

# Declare dataset 
panel = pd.Panel({'Fertility': gapminder.fertility, 
        'Population': gapminder.population, 
        'Life expectancy': gapminder.life_expectancy}) 
gapminder_df = panel.to_frame().reset_index().rename(columns={'minor': 'Year'}) 
gapminder_df = gapminder_df.merge(gapminder.regions.reset_index(), on='Country') 
gapminder_df['Country'] = gapminder_df['Country'].astype('str') 
gapminder_df['Group'] = gapminder_df['Group'].astype('str') 
gapminder_df.Year = gapminder_df.Year.astype('f') 
ds = hv.Dataset(gapminder_df) 

# Apply dimension labels and ranges 
kdims = ['Fertility', 'Life expectancy'] 
vdims = ['Country', 'Population', 'Group'] 
dimensions = { 
    'Fertility' : dict(label='Children per woman (total fertility)', range=(0, 10)), 
    'Life expectancy': dict(label='Life expectancy at birth (years)', range=(15, 100)), 
    'Population': ('population', 'Population') 
} 

# Create Points plotting fertility vs life expectancy indexed by Year 
gapminder_ds = ds.redim(**dimensions).to(hv.Points, kdims, vdims, 'Year') 

# Define annotations 
text = gapminder_ds.clone({yr: hv.Text(1.2, 25, str(int(yr)), fontsize=30) 
          for yr in gapminder_ds.keys()}) 

# Define options 
opts = {'plot': dict(width=1000, height=600,tools=['hover'], size_index='Population', 
        color_index='Group', size_fn=np.sqrt, title_format="{label}"), 
     'style': dict(cmap='Set1', size=0.3, line_color='black', alpha=0.6)} 
text_opts = {'style': dict(text_font_size='52pt', text_color='lightgray')} 


# Combine Points and Text 
(gapminder_ds({'Points': opts}) * text({'Text': text_opts})).relabel('Gapminder Demo')