API reference — I/O & presets¶
behaviz.io ¶
Unified, backend-agnostic figure output: bv.save and bv.canvas.
save dispatches to the active renderer, which validates the
backend/extension combination and raises BehavizSaveError otherwise.
canvas is a context manager that creates one figure, lets plot calls inside
the block omit ax= (they auto-target the block's axes), and saves and/or
shows the figure once on exit.
save ¶
save(fig, path, **kwargs)
Save fig to path using the active backend.
The format is taken from the file extension and validated against the
backend (e.g. .png/.pdf/.svg on matplotlib/seaborn, .html
on bokeh). Unsupported combinations raise
:class:~behaviz.core.errors.BehavizSaveError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Any
|
the figure handle returned by any plot function (the first element
of its |
required |
path
|
destination path; its extension selects the format. |
required | |
**kwargs
|
forwarded to the backend writer (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The path written, as a string. |
Source code in behaviz/io.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
canvas ¶
canvas(spec=None, *, ax=None, save=None, show=False, **save_kwargs)
Draw several plots onto one figure, then save/show it on exit.
Inside the block, plot calls that omit ax= automatically target the
block's axes (and inherit spec unless they pass their own). When the
block exits cleanly the figure is saved (if save is given) and shown
(if show is True). If the block raises, nothing is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Optional[PlotSpec]
|
the figure/axis specification for the shared axes. |
None
|
ax
|
Any
|
draw onto this existing axes instead of creating a figure — e.g. one
cell of a |
None
|
save
|
Optional[str]
|
optional path to write on exit (extension selects the format). |
None
|
show
|
bool
|
display the figure on exit. |
False
|
**save_kwargs
|
forwarded to :func: |
{}
|
Yields:
| Type | Description |
|---|---|
|
The shared axes object (also usable explicitly, e.g. for |
Example
with bv.canvas(save="fig.png") as ax: ... bv.plot_line(x, y) ... bv.plot_scatter(x, y2) f, axs = plt.subplots(1, 2) with bv.canvas(ax=axs[0]) as a: ... bv.plot_line(x, y)
Source code in behaviz/io.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
behaviz.presets ¶
behaviz_home ¶
behaviz_home()
Root behaviz config directory (~/.behaviz unless BEHAVIZ_HOME set).
Source code in behaviz/presets.py
16 17 18 19 | |
presets_dir ¶
presets_dir()
Directory holding user preset JSON files (on the load path).
Source code in behaviz/presets.py
22 23 24 | |
examples_dir ¶
examples_dir()
Directory holding read-only reference copies of the built-in presets.
This is not on the load path — files here are starting points to copy
into presets/ and edit, so they never shadow (or go stale against) the
in-code built-ins.
Source code in behaviz/presets.py
27 28 29 30 31 32 33 34 | |
init_home ¶
init_home(with_examples=True)
Scaffold the behaviz home directory.
Creates ~/.behaviz/presets/ and a README.txt, and (by default) writes
the built-in presets as reference copies into ~/.behaviz/examples/.
Deliberately does not seed presets/ with built-ins: doing so would
shadow the in-code built-ins permanently and freeze them at install time.
The examples/ copies are off the load path, so they are safe to refresh
and never go stale against an upgraded behaviz.
Idempotent — safe to re-run; it refreshes README/examples in place.
Returns¶
dict
{"home", "presets", "readme", "examples": [paths...]} describing what
was written, for callers (e.g. the CLI) to report.
Source code in behaviz/presets.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
save_preset ¶
save_preset(name, spec, overwrite=True)
Save a :class:PlotSpec as a user preset under ~/.behaviz/presets/.
Parameters¶
name : str
Plain preset name (no path separators) — becomes <name>.json.
spec : PlotSpec
The spec to persist. A post_hook, if set, is dropped (callables
cannot be serialized) with a warning.
overwrite : bool, default True
When False, raise FileExistsError instead of replacing an existing
user preset of the same name.
Returns¶
pathlib.Path The path the preset was written to.
Source code in behaviz/presets.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
export_preset ¶
export_preset(name, path)
Write a preset out to a standalone JSON file for sharing.
Unlike :func:save_preset (which writes into ~/.behaviz), this exports
to an arbitrary location so you can email/commit/copy the file to another
machine. Works for both user and built-in presets.
Parameters¶
name : str
Name of an existing preset (user or built-in).
path : str | pathlib.Path
Destination. If it points at an existing directory, the file is written
there as <name>.json; otherwise it is treated as the full file path.
Returns¶
pathlib.Path The path the preset was written to.
Source code in behaviz/presets.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
import_preset ¶
import_preset(path, name=None, overwrite=True)
Install a standalone preset JSON file into your ~/.behaviz library.
The reverse of :func:export_preset: after importing, the preset is
loadable by name via :func:load_preset.
Parameters¶
path : str | pathlib.Path
The preset file to import.
name : str, optional
Name to store it under. Defaults to the file's stem (cool.json →
"cool").
overwrite : bool, default True
When False, raise FileExistsError if a user preset of that name
already exists.
Returns¶
pathlib.Path
The path the preset was installed to inside ~/.behaviz/presets.
Source code in behaviz/presets.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
load_preset ¶
load_preset(name)
Load a preset by name and return a fresh :class:PlotSpec.
User presets take precedence over built-ins of the same name.
Source code in behaviz/presets.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
list_presets ¶
list_presets()
Return {name: source} for every available preset, sorted by name.
source is "builtin" or "user"; a user preset shadowing a
built-in is reported as "user".
Source code in behaviz/presets.py
257 258 259 260 261 262 263 264 265 266 267 | |
delete_preset ¶
delete_preset(name)
Delete a user preset. Built-ins cannot be deleted.
Source code in behaviz/presets.py
270 271 272 273 274 275 276 277 278 279 | |