Debian Codegraph @

skrf/notebook/bokeh_.py

raw symbols SCIP
1 from bokeh import models, plotting
2
3 from .. import network
4 from .utils import trace_color_cycle
5
6 plotting.output_notebook()
7
8 Y_LABEL_DICT = network.Y_LABEL_DICT
9 PRIMARY_PROPERTIES = network.PRIMARY_PROPERTIES
10 COMPONENT_FUNC_DICT = network.COMPONENT_FUNC_DICT
11
12 default_kwargs = {
13 'primary_property': "s",
14 'property_type': "db",
15 'show': True,
16 'fig': None
17 }
18
19
20 def plot_rectangular(ntwk, **kwargs):
21 """
22 :type ntwk: Network
23 :return: plotting.figure.Figure
24 """
25
26 fig = kwargs.get("fig", None)
27 show = kwargs.get("show", True)
28
29 primary_property = kwargs.get("primary_property", "s")
30 property_type = kwargs.get("property_type", "db")
31
32 colors = trace_color_cycle()
33
34 if type(fig) is not plotting.Figure:
35 fig = plotting.figure(
36 title=ntwk.name,
37 height=350, width=800,
38 x_axis_label=f"frequency ({ntwk.frequency.unit:s})",
39 y_axis_label=Y_LABEL_DICT[property_type],
40 tools="resize, pan, wheel_zoom, box_zoom, save, reset",
41 toolbar_location="above",
42 toolbar_sticky=True
43 )
44
45 labels = []
46 glyphs = []
47
48 for n in range(ntwk.nports):
49 for m in range(ntwk.nports):
50 x = ntwk.frequency.f_scaled
51 y = getattr(ntwk, primary_property + "_" + property_type)[:, m, n]
52 glyphs.append(fig.line(x, y, line_color=next(colors)))
53 labels.append(f"S{n + 1:d}{m + 1:d}")
54
55 legend_items = []
56 for label, glyph in zip(labels, glyphs):
57 legend_items.append((label, [glyph]))
58
59 legend = models.Legend(items=legend_items, location=(0, -30))
60
61 fig.add_layout(legend, 'right')
62
63 if show:
64 plotting.show(fig)
65
66 return fig
67
68
69 def plot_polar():
70 pass # not native to bokeh, but I have seen some hacks to do this. Smith chart may be tricky
71
72
73 def use_bokeh():
74 for p in PRIMARY_PROPERTIES:
75 for t in COMPONENT_FUNC_DICT.keys():
76 attribute_name = f"plot_{p:s}_{t:s}"
77
78 def gen_plot_function(p, t):
79 def plot_function(ntwk, **kwargs):
80 kwargs["primary_property"] = p
81 kwargs["property_type"] = t
82 plot_rectangular(ntwk, **kwargs)
83 return plot_function
84
85 setattr(network.Network, attribute_name, gen_plot_function(p, t))
86
87 use_bokeh() # this function can be called again if we need to switch plotting engines for some reason