# Show ECG curve plot

**URL:** <https://discourse.slicer.org/t/show-ecg-curve-plot/23291>\
**Category:** Development\
**Tags:** heart\
**Created:** [May 5, 2022, 6:58pm UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291 "2022-05-05T18:58:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![connorh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/connorh/32/79802_2.png) [@connorh](https://discourse.slicer.org/u/connorh)\
**Post date:** [May 5, 2022, 6:58pm UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/1 "2022-05-05T18:58:24Z")

</div>

Hi Robin,  
it has been a while but was this ever solved? I am wondering the same thing.

I have a 4D cardiac dataset and I also have ECG voltage tracings for a cardiac cycle. I would like to enable playback through my 4D cardiac dataset while showing where on the ECG voltage tracing we are. I figured this might be achieved through qMRMLPlotWidgets in my UI, but can’t figure out how to plot to them…

 ![4D Playback w ECG](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/7/9763a108d27701d32a0d708a25986000b8b77eff.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 5, 2022, 9:58pm UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/2 "2022-05-05T21:58:10Z")

</div>

You can read the ECG signals to a table node. See for example how it is done in the UltrasoundImage3D reader:

> <https://github.com/SlicerHeart/SlicerHeart/blob/bdbffde05b8193d04bf945bf4d5037c73b8d4a27/UltrasoundImage3dReader/UltrasoundImage3dReader.py#L190-L196>

You can find some more plotting examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#plots) and more explanations in the documentation ([user guide](https://slicer.readthedocs.io/en/latest/user_guide/modules/plots.html), [developer guide](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#plots)).

If you want to display a view outside the view layout (I would not recommend that, because it is probably better to choose or create an appropriate view layout instead) then you can follow this example:

 ![](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/8/580a2bb67a7dc750941c45d49e0c76f23a737a9b.png)

> [@Plotting the signals](https://discourse.slicer.org/t/plotting-the-signals/20242/12):
>
> I could not reproduce any crash with the scripts above, so maybe the issue is how you fill the tables with real data. I’ve made a couple of fixes and improvement (fix order of views, fit plot to the view, adjust labeling, etc.) and this is the result: The updated code: def SetLayout(nChannels): customLayout=( "\<layout type=\"horizontal\" split=\"true\"\>" "\<item\>" "\<layout type=\"vertical\" split=\"true\"\>" "\<item\>" "\<vi…

There is also a simpler example in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-a-slice-view-outside-the-view-layout).

To display a marker on the signal, I would recommend to add a series that has a single point or vertical line segment and add that to the chart.

---

<div class="post-metadata">

**Author:** ![connorh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/connorh/32/79802_2.png) [@connorh](https://discourse.slicer.org/u/connorh)\
**Post date:** [May 6, 2022, 8:12pm UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/4 "2022-05-06T20:12:21Z")

</div>

Thank you Dr. Lasso for the help! I guess my question is specific to the qMRMLPlotWidget, or something equivalent so that I can plot the ECG waveform within my extension UI. For example, using the QT designer I have added 3 qMRMLPlotWidgets to my UI, with the intent of plotting a 3-lead ECG overlaid with my playback tools:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/e/7e5b1bb77ef001aa636f48e95c4b59dd28a8bf49.png)

What I’m looking for is something similar to this line of code, except to plot a vtkMRMLPlotChartNode into my UI rather than the standard Slicer Plotting Layout.  
`slicer.modules.plots.logic().ShowChartInLayout(plotChartNode)`

I’ve tried to pull the equivalent of what I’m running within my extension:

```auto

viewNode = self.logic.createECGPlotNode()
self.ui.qMRMLPlotWidget1.setMRMLPlotViewNode(viewNode) #This is what doesn't seem to be working

def createECGPlotNode():

    #Create Dummy Data
    lead1 = np.random.randint(1,100,2000)
    lead1ecg = np.array(lead1)
    t_step = 0.01 #10ms steps
    t = np.linspace(0,len(lead1)*t_step,len(lead1))

    data_to_plot = np.column_stack((t,lead1ecg))

    tableNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", "ECGTable")
    slicer.util.updateTableFromArray(tableNode, data_to_plot)
    tableNode.GetTable().GetColumn(0).SetName("Time")
    tableNode.GetTable().GetColumn(1).SetName("Voltage")
    
    plotSeriesNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotSeriesNode", "PlotSeriesNode")
    plotSeriesNode.SetAndObserveTableNodeID(tableNode.GetID())
    plotSeriesNode.SetXColumnName("Time")
    plotSeriesNode.SetYColumnName("Voltage")
    plotSeriesNode.SetPlotType(plotSeriesNode.PlotTypeScatter)
    
    plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")
    plotChartNode.AddAndObservePlotSeriesNodeID(plotSeriesNode.GetID())

    viewNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotViewNode","ViewNode")
    viewNode.SetPlotChartNodeID(plotChartNode.GetID())

    #If I put slicer.modules.plots.logic().ShowChartInLayout(plotChartNode) here, then it plots properly to usual display

    return viewNode

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 10, 2022, 4:14am UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/5 "2022-05-10T04:14:21Z")

</div>

You need to make sure the layout manager does own your view nodes. See [example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-a-slice-view-outside-the-view-layout).

---

<div class="post-metadata">

**Author:** ![whu](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@whu](https://discourse.slicer.org/u/whu)\
**Post date:** [June 11, 2022, 4:54am UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/6 "2022-06-11T04:54:59Z")

</div>

Dear Sir,  
once the 4D data was loaded into slicer, how to reference the data using python script?

> [@lassoan](#):
>
> `ecg = source.GetECG()`

especially the source ? We don’t need to reload the data as the example .

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 12, 2022, 1:17am UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/7 "2022-06-12T01:17:30Z")

</div>

What file format did you load your data from?

The example above was for Image3dAPI reader, which has that `source` object. You don’t need to use that object because the reader creates a table node from the ECG data that you can easily visualize using Plots module.

If you are interested in showing ECG then you may join @connorh at the upcoming [project week](https://projectweek.na-mic.org/) to work on cardiac data reading and visualization.

---

<div class="post-metadata">

**Author:** ![whu](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@whu](https://discourse.slicer.org/u/whu)\
**Post date:** [June 12, 2022, 1:11pm UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/8 "2022-06-12T13:11:00Z")

</div>

The DICOM file exported by QLab, and patched in Slicer.  
thanks, I will try to focus on the project week.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 13, 2022, 4:30am UTC](https://discourse.slicer.org/t/show-ecg-curve-plot/23291/9 "2022-06-13T04:30:39Z")

</div>

Unfortunately, the Cartesian images exported by QLab do not contain any ECG data.
