Simplify plot creation in panel_ccl_integrate

This commit is contained in:
usov_i 2022-12-14 16:45:10 +01:00
parent f5814ddd5e
commit 23ba256b6e

View File

@ -8,40 +8,29 @@ import numpy as np
from bokeh.io import curdoc from bokeh.io import curdoc
from bokeh.layouts import column, row from bokeh.layouts import column, row
from bokeh.models import ( from bokeh.models import (
BasicTicker,
Button, Button,
CellEditor, CellEditor,
CheckboxEditor, CheckboxEditor,
CheckboxGroup, CheckboxGroup,
ColumnDataSource, ColumnDataSource,
CustomJS, CustomJS,
DataRange1d,
DataTable, DataTable,
Div, Div,
Dropdown, Dropdown,
FileInput, FileInput,
Grid,
Legend,
Line,
LinearAxis,
MultiLine,
MultiSelect, MultiSelect,
NumberEditor, NumberEditor,
Panel, Panel,
PanTool,
Plot,
RadioGroup, RadioGroup,
ResetTool,
Scatter,
Select, Select,
Spacer, Spacer,
Span, Span,
Spinner, Spinner,
TableColumn, TableColumn,
TextAreaInput, TextAreaInput,
WheelZoomTool,
Whisker, Whisker,
) )
from bokeh.plotting import figure
import pyzebra import pyzebra
from pyzebra import AREA_METHODS, EXPORT_TARGETS from pyzebra import AREA_METHODS, EXPORT_TARGETS
@ -258,12 +247,12 @@ def create():
x = scan[scan_motor] x = scan[scan_motor]
plot.axis[0].axis_label = scan_motor plot.axis[0].axis_label = scan_motor
plot_scatter_source.data.update(x=x, y=y, y_upper=y + y_err, y_lower=y - y_err) scatter_source.data.update(x=x, y=y, y_upper=y + y_err, y_lower=y - y_err)
fit = scan.get("fit") fit = scan.get("fit")
if fit is not None: if fit is not None:
x_fit = np.linspace(x[0], x[-1], 100) x_fit = np.linspace(x[0], x[-1], 100)
plot_fit_source.data.update(x=x_fit, y=fit.eval(x=x_fit)) fit_source.data.update(x=x_fit, y=fit.eval(x=x_fit))
x_bkg = [] x_bkg = []
y_bkg = [] y_bkg = []
@ -279,49 +268,40 @@ def create():
xs_peak.append(x_fit) xs_peak.append(x_fit)
ys_peak.append(comps[f"f{i}_"]) ys_peak.append(comps[f"f{i}_"])
plot_bkg_source.data.update(x=x_bkg, y=y_bkg) bkg_source.data.update(x=x_bkg, y=y_bkg)
plot_peak_source.data.update(xs=xs_peak, ys=ys_peak) peak_source.data.update(xs=xs_peak, ys=ys_peak)
fit_output_textinput.value = fit.fit_report() fit_output_textinput.value = fit.fit_report()
else: else:
plot_fit_source.data.update(x=[], y=[]) fit_source.data.update(x=[], y=[])
plot_bkg_source.data.update(x=[], y=[]) bkg_source.data.update(x=[], y=[])
plot_peak_source.data.update(xs=[], ys=[]) peak_source.data.update(xs=[], ys=[])
fit_output_textinput.value = "" fit_output_textinput.value = ""
# Main plot # Main plot
plot = Plot( plot = figure(
x_range=DataRange1d(), x_axis_label="Scan motor",
y_range=DataRange1d(only_visible=True), y_axis_label="Counts",
plot_height=470, plot_height=470,
plot_width=700, plot_width=700,
tools="pan,wheel_zoom,reset",
) )
plot.add_layout(LinearAxis(axis_label="Counts"), place="left") scatter_source = ColumnDataSource(dict(x=[0], y=[0], y_upper=[0], y_lower=[0]))
plot.add_layout(LinearAxis(axis_label="Scan motor"), place="below") plot.circle(
source=scatter_source, line_color="steelblue", fill_color="steelblue", legend_label="data"
plot.add_layout(Grid(dimension=0, ticker=BasicTicker()))
plot.add_layout(Grid(dimension=1, ticker=BasicTicker()))
plot_scatter_source = ColumnDataSource(dict(x=[0], y=[0], y_upper=[0], y_lower=[0]))
plot_scatter = plot.add_glyph(
plot_scatter_source, Scatter(x="x", y="y", line_color="steelblue", fill_color="steelblue")
) )
plot.add_layout(Whisker(source=plot_scatter_source, base="x", upper="y_upper", lower="y_lower")) plot.add_layout(Whisker(source=scatter_source, base="x", upper="y_upper", lower="y_lower"))
plot_fit_source = ColumnDataSource(dict(x=[0], y=[0])) fit_source = ColumnDataSource(dict(x=[0], y=[0]))
plot_fit = plot.add_glyph(plot_fit_source, Line(x="x", y="y")) plot.line(source=fit_source, legend_label="best fit")
plot_bkg_source = ColumnDataSource(dict(x=[0], y=[0])) bkg_source = ColumnDataSource(dict(x=[0], y=[0]))
plot_bkg = plot.add_glyph( plot.line(source=bkg_source, line_color="green", line_dash="dashed", legend_label="linear")
plot_bkg_source, Line(x="x", y="y", line_color="green", line_dash="dashed")
)
plot_peak_source = ColumnDataSource(dict(xs=[[0]], ys=[[0]])) peak_source = ColumnDataSource(dict(xs=[[0]], ys=[[0]]))
plot_peak = plot.add_glyph( plot.multi_line(source=peak_source, line_color="red", line_dash="dashed", legend_label="peak")
plot_peak_source, MultiLine(xs="xs", ys="ys", line_color="red", line_dash="dashed")
)
fit_from_span = Span(location=None, dimension="height", line_dash="dashed") fit_from_span = Span(location=None, dimension="height", line_dash="dashed")
plot.add_layout(fit_from_span) plot.add_layout(fit_from_span)
@ -329,21 +309,9 @@ def create():
fit_to_span = Span(location=None, dimension="height", line_dash="dashed") fit_to_span = Span(location=None, dimension="height", line_dash="dashed")
plot.add_layout(fit_to_span) plot.add_layout(fit_to_span)
plot.add_layout( plot.y_range.only_visible = True
Legend(
items=[
("data", [plot_scatter]),
("best fit", [plot_fit]),
("peak", [plot_peak]),
("linear", [plot_bkg]),
],
location="top_left",
click_policy="hide",
)
)
plot.add_tools(PanTool(), WheelZoomTool(), ResetTool())
plot.toolbar.logo = None plot.toolbar.logo = None
plot.legend.click_policy = "hide"
# Scan select # Scan select
def scan_table_select_callback(_attr, old, new): def scan_table_select_callback(_attr, old, new):