Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Last active January 25, 2022 13:46
Show Gist options
  • Save jj-github-jj/efce419856b333e52419cbcef282efae to your computer and use it in GitHub Desktop.
Save jj-github-jj/efce419856b333e52419cbcef282efae to your computer and use it in GitHub Desktop.
holoviews examples
#https://notebook.community/redlegjed/cheatsheets/holoviews_cheatsheet
#bokeh figure from holoview
bf=hv.render(p) #bf is bokeh figure object
bf
def formatter(value):
return str(value) + ' days'
curve.relabel('Tick formatters').opts(xformatter=formatter, yformatter='$%.2f', width=500)
x = np.linspace(0,4*np.pi)
y = np.cos(x)
wave = hv.Curve((x, y), ('x','x axis'), ('y','y axis'))
wave
x,y = map(list, zip(*tl)) #map list of tuples to two lists. * unpacks the tuple
p=hv.Curve((f,P)).opts(tools=['hover']) #frequency, power arrays
p=p.opts(xlim=(0, 1e9), ylim=(-220, 20)) # set limits
x,y = map(np.asarray, zip(*tl)) #map list of tuples to two np arrays. * unpacks the tuple
#Formatting Engg notation
from bokeh.models import FuncTickFormatter
formatter = FuncTickFormatter(code='''
var space = ' ';
// Giga
if(tick >= 1e10)
return (tick / 1e9).toFixed(1) + space + 'G';
else if(tick >= 1e9)
return (tick / 1e8).toFixed(0) / 10 + space + 'G';
// Mega
else if(tick >= 1e7)
return (tick / 1e6).toFixed(1) + space + 'M';
else if(tick >= 1e6)
return (tick / 1e5).toFixed(0) / 10 + space + 'M';
// Kilo
else if(tick >= 1e4)
return (tick / 1e3).toFixed(1) + space + 'k';
else if(tick >= 1e3)
return (tick / 1e2).toFixed(0) / 10 + space + 'k';
// Unit
else if(tick >= 1e1)
return (tick / 1e0).toFixed(0) + space + '';
else
return (tick / 1e-1).toFixed(0) / 10 + space + '';
''')
bf.xaxis.formatter=formatter #java script function for engg format x axis display
show(bf)
#customize tooltips in holoview https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#numeraltickformatter
from bokeh.models import HoverTool
tooltips = [ #customize tooltips
('fstimulus', '@fstimulus{0,0}'),
]
hover = HoverTool(tooltips=tooltips)
#%%opts heatmap [tools=[hover]]
ds.hvplot.heatmap(x='fstimulus',y='freq',C='p').opts(xformatter=jsformatter,
yformatter=jsformatter,tools=[hover]) #jsformatter is a javascript formatter defined earlier
HoverTool(
tooltips=[
( 'date', '@date{%F}' ),
( 'close', '$@{adj close}{%0.2f}' ), # use @{ } for field names with spaces
( 'volume', '@volume{0.00 a}' ),
],
formatters={
'@date' : 'datetime', # use 'datetime' formatter for '@date' field
'@{adj close}' : 'printf', # use 'printf' formatter for '@{adj close}' field
# use default 'numeral' formatter for other fields
},
# display a tooltip whenever the cursor is vertically in line with a glyph
mode='vline'
)
#https://discourse.bokeh.org/t/hovertool-help-custom-tooltip/7038/3
#peak holoviews xy plot
peak=p1.data.y.max()
x_y_peak=(p1.data.x[p1.data.y.idxmax()],p1.data.y.max())
label_list=[ str(en(x_y_peak[1])) + '\n'+ str( en(x_y_peak[0] )) ]
data=np.asarray( (x_y_peak[0] , x_y_peak[1] ) )
data=data.reshape(1,2)
labels = hv.Labels({('x', 'y'): data, 'text': label_list}, ['x', 'y'], 'text').opts (text_font_size='7pt',
xoffset=0.08,tools=['hover'],width=600)
labels*p1
#use dataframe for labels
label_df=pd.DataFrame()
label_df['text']= [str(en(x_y_peak[1])) + '\n'+ str( en(x_y_peak[0] )) ] #pass alist of column values
label_df['x']=x_y_peak[0];label_df['y']=x_y_peak[1]
# label_df.x=x_y_peak[0] ;label_df.y=x_y_peak[1]
label_df
lo=label_df.hvplot.labels(x='x',y='y',text='text')
lo*p1 #overlay label object with plot
def hv_label_plot_df (x=[],y=[],labels=[],text_font_size='7pt'):
label_df=pd.DataFrame()
label_df['text']= labels #pass alist of column values
label_df['x']=x;label_df['y']=y
# label_df.x=x_y_peak[0] ;label_df.y=x_y_peak[1]
label_plot=label_df.hvplot.labels(x='x',y='y',text='text')\
.opts( text_font_size=text_font_size
# 'title': '100%',
# 'labels': '100%',
# 'ticks': '100%', }
)
return (label_df,label_plot)
labels=[str(en(x_y_peak[1])) + '\n'+ str( en(x_y_peak[0] )) ]
label_df,label_plot=hv_label_plot_df(x=[ x_y_peak[0]],y=[ x_y_peak[1] ],labels=labels,text_font_size='8pt')
label_plot*p1+label_df.hvplot.table() #generates a slick grid dataframe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment