bokeh 기본 사용법

!·2023년 11월 12일
0

시각화

목록 보기
1/2
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show

TOOLS = "box_select, lasso_select, reset, undo, redo, box_zoom, zoom_in, zoom_out, save, pan, hover"

source = ColumnDataSource(df)

left = figure(width=800, height=600, title=None, tools=TOOLS, active_drag="lasso_select", y_range=(7.2,7.5), x_range=(-100,5500))
left.scatter("x1", "x2", source=source)

right = figure(width=1000, height=600, title=None, tools=TOOLS, active_drag="lasso_select", y_axis_location="right", x_range=(df.ts[0], df.ts.iloc[-1]), x_axis_type="datetime")
right.scatter("ts", "x1", source=source)
right.line("ts", "x1", source=source)

show(gridplot([[left, right]]))
from bokeh.io import output_notebook, push_notebook
output_notebook()

from bokeh.layouts import column, row
from bokeh.models import Slider, Span, Range1d
from bokeh.plotting import figure, show
from bokeh.palettes import cividis

from ipywidgets import interact, interactive, widgets

plot = figure(width=800, height=400)
x = np.linspace(0, 5*np.pi, 500)
color = cividis(5)
sine = plot.line(x, np.sin(x), line_width=1, alpha=0.8, line_color=color[0], legend_label="sin")
cosine = plot.line(x, np.cos(x), line_width=1, alpha=0.8, line_color=color[3], legend_label="cos")
vline = Span(location=0, dimension="height", line_color=color[2], line_width=3, line_alpha=0.5)
hline = Span(location=0, dimension="width", line_color=color[2], line_width=3, line_alpha=0.5)
plot.add_layout(vline)
plot.add_layout(hline)
plot.title.text = "Sine and cosine"
plot.legend.click_policy = "hide"
plot.legend.location = "top_left"
plot.xaxis.axis_label = "x"
plot.yaxis.axis_label = "y"
plot.y_range = Range1d(-4, 4)
handle = show(plot, notebook_handle=True)

# Slider: Using ipython widgets slider instead of Bokeh slider
@interact(w=widgets.FloatSlider(min=-10, max=10, value=1),
          amp=widgets.FloatSlider(min=-5, max=5, value=1),
          phi=widgets.FloatSlider(min=-4, max=4, value=0))
def update(w=1.0, amp=1, phi=0):
    sine.data_source.data["y"] = amp*np.sin(w*x-phi)
    cosine.data_source.data["y"] = amp*np.cos(w*x-phi)
    vline.location = phi
    hline.location = amp*np.sin(-phi)
    push_notebook(handle=handle)

출처: https://www.adrian.idv.hk/2021-07-13-jupyter/

from bokeh.io import output_notebook, push_notebook
output_notebook()
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure, show
from ipywidgets import interact, widgets

TOOLS = "box_select, lasso_select, reset, undo, redo, box_zoom, zoom_in, zoom_out, save, pan, hover"

source = ColumnDataSource(tmp)

left = figure(width=800, height=600, title=None, tools=TOOLS, active_drag="lasso_select", y_range=(7.2,7.5), x_range=(-100,5500))
left.scatter("x1", "x2", source=source)

right = figure(width=1000, height=600, title=None, tools=TOOLS, active_drag="lasso_select", y_axis_location="right", x_range=(df.ts[0], df.ts.iloc[-1]), x_axis_type="datetime")
right.scatter("ts", "x1", source=source)
right.line("ts", "x2", source=source)

handle = show(gridplot([[left, right]]), notebook_handle=True)

###
n_slider = Slider(start=0, end=tmp['n'].max(), value=1, step=1, title='Number')

@interact(n=widgets.IntSlider(min=0, max=2, value=1))
def on_change_n(n=1):
    new_data = df[df['n'] == n]
    source.data = new_data
    push_notebook(handle=handle)
profile
AI 기술로 먹고 살기

0개의 댓글