# Python bridge plotting example

**URL:** <https://mworks.discourse.group/t/python-bridge-plotting-example/448>\
**Category:** Support\
**Created:** [May 2, 2016, 3:39pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448 "2016-05-02T15:39:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 3:39pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/1 "2016-05-02T15:39:58Z")

</div>

Hi Mark,

As you requested last time we spoke, I’ve put together a small example of how I generate trial-by-trial plots using the Python bridge.

The attached files are an adaption of the [MATLAB window demo](https://mworks.discourse.group/kb/data-analysis/using-the-matlab-window) to Python. The experiment generates random values (10 per trial over 10 trials) and stores them in the variable `rand_var`. The Python script draws a histogram of these values, updating the plot whenever new values are received. The script can be run via the client or server bridge. (In the latter case, the conduit resource is assumed to be named `server_conduit`.)

The `Conduit` class defined in common.py provides a generic framework for online plots. It inherits from `mworks.conduit.IPCAccumClientConduit`, which accumulates requested events between “start” and “stop” events, then delivers them as a group to an event handler. (The Conduit class expects variables named `start` and `stop` for this purpose, but this can be changed to any two variables you prefer.)

The main benefit of the Conduit class is that it handles running of the application main loop (starting and stopping it as needed to handle new events). This keeps the matplotlib window responsive to user input, allowing you to pan, zoom, or whatever.

If you have any questions, please let me know.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 3:39pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/2 "2016-05-02T15:39:58Z")

</div>

Attachment: [python\_bridge\_plotting\_demo.xml](https://mworks.discourse.group/uploads/short-url/o94Io0PX80GfwjTkaYMfo80gcIP.xml) (1.87 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 3:40pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/3 "2016-05-02T15:40:31Z")

</div>

Python files attached

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 3:40pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/4 "2016-05-02T15:40:31Z")

</div>

Attachments:

- [python\_bridge\_plotting\_demo.py](https://mworks.discourse.group/uploads/short-url/taAFyDNG4DcBbaGVgmWODKRtCSR.py) (572 Bytes)
- [common.py](https://mworks.discourse.group/uploads/short-url/dQlkGXVeXzDWIohZJFvNh2uoTu2.py) (1.61 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 5:22pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/5 "2016-05-02T17:22:22Z")

</div>

Thanks, Chris. I will look at this, it’s very helpful.

What Python distribution/version are you using? Can I test it on a recent version of anaconda with Python 2.7? That is our preferred option now.

Mark

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 7:03pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/6 "2016-05-02T19:03:26Z")

</div>

> What Python distribution/version are you using? Can I test it on a recent version of anaconda with Python 2.7? That is our preferred option now.

I normally use the system Python (/usr/bin/python2.7), but if you’re running the script via the server conduit, any Python 2.7 should be fine. (I just tested it with Anaconda, and it worked as expected.)

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 2, 2016, 7:21pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/7 "2016-05-02T19:21:23Z")

</div>

Great. We will need scipy for things like smoothing data, and scipy isn’t installed with the system python. And it’s a pain to install last I checked, I believe it needs Fortran code compiled. Anaconda is a good solution for us.

Mark

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 10:04pm UTC](https://mworks.discourse.group/t/python-bridge-plotting-example/448/8 "2022-07-19T22:04:19Z")

</div>

Closed on July 19, 2022, 10:04pm UTC


