Real time interactive plot in Slicer

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:

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

I went through several examples, like:

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.

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"])

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.