# Real time interactive plot in Slicer

**URL:** <https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494>\
**Category:** Development\
**Created:** [March 2, 2020, 3:11pm UTC](https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494 "2020-03-02T15:11:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpsiddiqui91](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@fpsiddiqui91](https://discourse.slicer.org/u/fpsiddiqui91)\
**Post date:** [March 2, 2020, 3:11pm UTC](https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494/1 "2020-03-02T15:11:05Z")

</div>

Dear All,

I have been working with slicer for 5-6 months. I have managed to develop some useful modules. However, for the current task, I will be needing your help.

I have my data in NumPy array, which is kept on updating progressively. I need to plot a histogram real-time with an option that a user can interact with a histogram or make a rectangle or something to select a art of the data

something like this:

> [@New feature: Histogram for setting threshold range in Segment Editor](https://discourse.slicer.org/t/new-feature-histogram-for-setting-threshold-range-in-segment-editor/9221):
>
> In the latest 3D Slicer 4.11.0 releases, the Threshold effect in the Segment Editor module now includes the option to specify the minimum and maximum thresholds based on the histogram of a region of interest in the slice view. [[Segment Editor: Local histogram in threshold effect] ](https://www.youtube.com/watch?v=pGVbhmsyXhY) Clicking and dragging on a slice view will create a yellow border that encompasses the region of interest. Several different shapes can be used to draw the region of interest including: box, circle, freefo…

I cannot find the code for this, can you help me making this king of the histogram in the most efficient way?

---

<div class="post-metadata">

**Author:** ![fpsiddiqui91](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@fpsiddiqui91](https://discourse.slicer.org/u/fpsiddiqui91)\
**Post date:** [March 2, 2020, 6:07pm UTC](https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494/2 "2020-03-02T18:07:57Z")

</div>

I went through several examples, like:

> [@Area of segment in a given slice?](https://discourse.slicer.org/t/area-of-segment-in-a-given-slice/1559):
>
> Is there a way to determine the area a segment takes up in a given slice? I’m interested in generating a plot from a CT scan, anterior to posterior, of the area taken up by a segment slice-by-slice (if that makes sense). If you were to do this on a sphere, for example, you’d wind up with a gaussian. (I think, I’d have to double-check the math on that…) Thanks! -Hollister

> [@How can I run the Mesh statistics?](https://discourse.slicer.org/t/how-can-i-run-the-mesh-statistics/2760/2):
>
> You can simply save the file in vtk or vtp file format and that will contain all the computed distances for each point. Note that you can also access all the distance values in Python and compute statistics (histogram, etc). For example, this script shows how to compute and plot histogram: modelNode = getNode('VTK Output File') # Get distances from model node (stored in point data array) import vtk.util.numpy\_support distanceArrayVtk =modelNode.GetPolyData().GetPointData().GetArray('Signed') …

I managed to make a histogram with my Table Node, However, as I update the table Node with my new data, the chart just vanishes. Here is my code for making a histogram:

```
self.plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode", "Histogram")
histogram = np.histogram(self.my_DATA, bins=10) # my_DATA is the numpy array containing the data
self.tableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode","Distance Histogram")
slicer.util.updateTableFromArray(self.tableNode,histogram)
self.tableNode.GetTable().GetColumn(0).SetName("Count")
self.tableNode.GetTable().GetColumn(1).SetName("Intensity")

# Create new plot data series node
self.plotSeriesNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotSeriesNode", "Distance histogram")
self.plotSeriesNode.SetAndObserveTableNodeID(self.tableNode.GetID())
self.plotSeriesNode.SetXColumnName("Intensity")
self.plotSeriesNode.SetYColumnName("Count")
self.plotSeriesNode.SetPlotType(slicer.vtkMRMLPlotSeriesNode.PlotTypeScatterBar)
self.plotSeriesNode.SetMarkerStyle(slicer.vtkMRMLPlotSeriesNode.MarkerStyleNone)

# Add plot to chart
self.plotChartNode.AddAndObservePlotSeriesNodeID(self.plotSeriesNode.GetID())
#self.plotChartNode.AddAndObservePlotDataNodeID(self.DataNode.GetID())

layoutManager=slicer.app.layoutManager()
layoutManager.setLayout (slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)
self.plotWidget = layoutManager.plotWidget(0)

self.plotViewNode= self.plotWidget.mrmlPlotViewNode()
self.plotViewNode.SetPlotChartNodeID (self.plotChartNode.GetID())

```

This works fine. However as , my data gets updated, I make a new histogram and update the table node with a new histogram data as:

```
histogram = np.histogram(self.my_DATA, bins=20)

slicer.util.updateTableFromArray(self.tableNode, histogram)
self.plotWidget.update()

```

As soon as my Table node gets updated, I cannot see anything in the chart.  
Can you help me with this? @lassoan may be. Thanks.

Thanks in advance.

---

<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:** [March 3, 2020, 2:08am UTC](https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494/3 "2020-03-03T02:08:03Z")

</div>

The plot disappeared because you specified that the chart is built from columns “Intensity” and “Count”, but the table after calling `slicer.util.updateTableFromArray(tableNode, histogram)` had default column names (Column 1, Column 2). Since numpy arrays do not store column names, you need to manage them by yourself and pass it to the the converter function yourself:

```
slicer.util.updateTableFromArray(tableNode, histogram, ["Count", "Intensity"])
```

---

<div class="post-metadata">

**Author:** ![fpsiddiqui91](https://avatars.discourse-cdn.com/v4/letter/f/53a042/32.png) [@fpsiddiqui91](https://discourse.slicer.org/u/fpsiddiqui91)\
**Post date:** [March 3, 2020, 7:41am UTC](https://discourse.slicer.org/t/real-time-interactive-plot-in-slicer/10494/4 "2020-03-03T07:41:13Z")

</div>

Thank you so much for your response @lassoan, it worked like a charm.

According to your experience, what is the most efficient way to make a real time interactive plot? Is there any better way other than this?

Thank you.
