-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e6cb17
commit 3a47398
Showing
5 changed files
with
120 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# `matplotlib` helpers | ||
|
||
When attempting to use `matplotlib` UI functions directly in a plan, and running `matplotlib` using a `Qt` | ||
backend (e.g. in a standalone shell outside IBEX), you may see an error of the form: | ||
|
||
``` | ||
UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail. | ||
fig, ax = plt.subplots() | ||
``` | ||
|
||
This is because the `RunEngine` runs plans in a worker thread, not in the main thread, which then requires special | ||
handling when calling functions that will update a UI. | ||
|
||
The following plan stubs provide Qt-safe wrappers around some matplotlib functions to avoid this error. | ||
|
||
```{note} | ||
Callbacks such as `LivePlot` and `LiveFitPlot` already route UI calls to the appropriate UI thread by default. | ||
The following plan stubs are only necessary if you need to call functions which will create or update a matplotlib | ||
plot from a plan directly. | ||
``` | ||
|
||
## `matplotlib_subplots` | ||
|
||
The {py:obj}`ibex_bluesky_core.plan_stubs.matplotlib_subplots` plan stub is a Qt-safe wrapper | ||
around `matplotlib.pyplot.subplots()`. It allows the same arguments and keyword-arguments as the | ||
underlying matplotlib function. | ||
|
||
Usage example: | ||
|
||
```python | ||
from ibex_bluesky_core.plan_stubs import matplotlib_subplots | ||
from ibex_bluesky_core.callbacks.plotting import LivePlot | ||
from bluesky.callbacks import LiveFitPlot | ||
from bluesky.preprocessors import subs_decorator | ||
|
||
def my_plan(): | ||
# BAD | ||
# fig, ax = plt.subplots() | ||
|
||
# GOOD | ||
fig, ax = yield from matplotlib_subplots() | ||
|
||
# Pass the matplotlib ax object to other callbacks | ||
@subs_decorator([ | ||
LiveFitPlot(..., ax=ax), | ||
LivePlot(..., ax=ax), | ||
]) | ||
def inner_plan(): | ||
... | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters