# How to get the histogram and area

**URL:** https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027
**Category:** Development
**Tags:** python
**Created:** [February 17, 2021, 7:10am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027 "2021-02-17T07:10:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![BORIPHAT](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BORIPHAT](https://discourse.slicer.org/u/BORIPHAT)
#### Post date: [February 17, 2021, 7:10am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/1 "2021-02-17T07:10:01Z")

</div>

Hello,  
I would like to get the histogram and the area. In the beginning, I follow the Calculating distance using Maurer distance map Topic. But when I copy and paste the script into the Python console. I got the below message. Could you please give me some advice? Thank you very much.

 ![Picture1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/5/f58e35fef6978eabaccecedfeb07b508db048f02.png)

```auto
Python 2.7.13 (default, May 16 2019, 14:27:45) [MSC v.1900 64 bit (AMD64)] on win32
>>> modelNode = getNode('VTK Output File')
DLL load failed: The specified module could not be found.
DLL load failed: The specified module could not be found.
DLL load failed: The specified module could not be found.
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Slicer 4.10.2\bin\Python\slicer\util.py", line 710, in getNode
    raise MRMLNodeNotFoundException("could not find nodes in the scene by name or id '%s'" % (pattern if (type(pattern) == str) else ""))
MRMLNodeNotFoundException: could not find nodes in the scene by name or id 'VTK Output File'
>>> distanceArrayName = "Signed"
>>> 
>>> # Get distances from point data
>>> import numpy as np
>>> distances = vtk.util.numpy_support.vtk_to_numpy(modelNode.GetPolyData().GetPointData().GetArray(distanceArrayName))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'modelNode' is not defined
>>> 
>>> # Print basic stats
>>> print("Minimum distance: %f" % min(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> print("Maximum distance: %f" % max(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> print("Mean distance: %f" % np.mean(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> 
>>> # Compute histogram
>>> histogram = np.histogram(distances, bins=100)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> 
>>> # Save results to a new table node
>>> tableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", modelNode.GetName() + " histogram")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'modelNode' is not defined
>>> updateTableFromArray(tableNode, histogram)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> tableNode.GetTable().GetColumn(0).SetName("Count")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> tableNode.GetTable().GetColumn(1).SetName("Intensity")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> 
>>> # Create plot
>>> 
>>> plotDataNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotDataNode")
>>> plotDataNode.SetAndObserveTableNodeID(tableNode.GetID())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetAndObserveTableNodeID'
>>> plotDataNode.SetXColumnName("Intensity")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetXColumnName'
>>> plotDataNode.SetYColumnName("Count")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetYColumnName'
>>> 
>>> plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")
>>> plotChartNode.AddAndObservePlotDataNodeID(plotDataNode.GetID())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'MRMLCorePython.vtkMRMLPlotChartNode' object has no attribute 'AddAndObservePlotDataNodeID'
>>> plotChartNode.SetAttribute("Type", "Bar") # delete this line for line plot
>>> 
>>> # Show plot in layout
>>> 
>>> layoutManager = slicer.app.layoutManager()
>>> layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)
>>> plotWidget = layoutManager.plotWidget(0)
>>> 
>>> plotViewNode = plotWidget.mrmlPlotViewNode()
>>> plotViewNode.SetPlotChartNodeID(plotChartNode.GetID())

```

---

<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: [February 17, 2021, 11:39am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/2 "2021-02-17T11:39:38Z")

</div>

This script is not compatible with Slicer 4.10. Let us know if you have any trouble with the latest stable version.

---

<div class="post-metadata">

### Author: ![BORIPHAT](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BORIPHAT](https://discourse.slicer.org/u/BORIPHAT)
#### Post date: [February 18, 2021, 6:16am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/3 "2021-02-18T06:16:58Z")

</div>

> [@BORIPHAT](#):
>
> Calculating distance using Maurer distance map

Thank you very much for your quick response. Now I use version 4.11 and I found the error again and the software does not show the Histogram. ![Picture3](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/2/92e4527b94a9a9bd8cc5c2fdce39a5a7b545ecf9.png)

---

<div class="post-metadata">

### Author: ![BORIPHAT](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BORIPHAT](https://discourse.slicer.org/u/BORIPHAT)
#### Post date: [February 18, 2021, 6:17am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/4 "2021-02-18T06:17:47Z")

</div>

```auto
Python 3.6.7 (default, Sep 30 2020, 16:13:32) [MSC v.1924 64 bit (AMD64)] on win32
>>> modelNode = getNode('VTK Output File')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Boriphat\AppData\Local\NA-MIC\Slicer 4.11.20200930\bin\Python\slicer\util.py", line 1137, in getNode
    raise MRMLNodeNotFoundException("could not find nodes in the scene by name or id '%s'" % (pattern if (isinstance(pattern, str)) else ""))
slicer.util.MRMLNodeNotFoundException: could not find nodes in the scene by name or id 'VTK Output File'
>>> distanceArrayName = "Signed"
>>> 
>>> # Get distances from point data
>>> import numpy as np
>>> distances = vtk.util.numpy_support.vtk_to_numpy(modelNode.GetPolyData().GetPointData().GetArray(distanceArrayName))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'modelNode' is not defined
>>> 
>>> # Print basic stats
>>> print("Minimum distance: %f" % min(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> print("Maximum distance: %f" % max(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> print("Mean distance: %f" % np.mean(distances))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> 
>>> # Compute histogram
>>> histogram = np.histogram(distances, bins=100)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'distances' is not defined
>>> 
>>> # Save results to a new table node
>>> tableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", modelNode.GetName() + " histogram")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'modelNode' is not defined
>>> updateTableFromArray(tableNode, histogram)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> tableNode.GetTable().GetColumn(0).SetName("Count")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> tableNode.GetTable().GetColumn(1).SetName("Intensity")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'tableNode' is not defined
>>> 
>>> # Create plot
>>> 
>>> plotDataNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotDataNode")
>>> plotDataNode.SetAndObserveTableNodeID(tableNode.GetID())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetAndObserveTableNodeID'
>>> plotDataNode.SetXColumnName("Intensity")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetXColumnName'
>>> plotDataNode.SetYColumnName("Count")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'SetYColumnName'
>>> 
>>> plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")
>>> plotChartNode.AddAndObservePlotDataNodeID(plotDataNode.GetID())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'MRMLCorePython.vtkMRMLPlotChartNode' object has no attribute 'AddAndObservePlotDataNodeID'
>>> plotChartNode.SetAttribute("Type", "Bar") # delete this line for line plot
>>> 
>>> # Show plot in layout
>>> 
>>> layoutManager = slicer.app.layoutManager()
>>> layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)
>>> plotWidget = layoutManager.plotWidget(0)
>>> 
>>> plotViewNode = plotWidget.mrmlPlotViewNode()
>>> plotViewNode.SetPlotChartNodeID(plotChartNode.GetID())
>>> 
>>>

```

---

<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: [February 18, 2021, 1:15pm UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/5 "2021-02-18T13:15:43Z")

</div>

> [@BORIPHAT](#):
>
> `modelNode = getNode('VTK Output File')`

Replace `VTK Output File` in the example with the actual name of your node.

---

<div class="post-metadata">

### Author: ![BORIPHAT](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@BORIPHAT](https://discourse.slicer.org/u/BORIPHAT)
#### Post date: [February 19, 2021, 5:51am UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/6 "2021-02-19T05:51:46Z")

</div>

I replace the VTK output File follow your recommendation and It shows the message. Could you please give me some advice again? Thank you very much for your kindness.

```auto
Python 3.6.7 (default, Sep 30 2020, 16:13:32) [MSC v.1924 64 bit (AMD64)] on win32
>>> modelNode = getNode('VTK Output File_6-0')distanceArrayName = "Signed"# Get distances from point dataimport numpy as npdistances = vtk.util.numpy_support.vtk_to_numpy(modelNode.GetPolyData().GetPointData().GetArray(distanceArrayName))# Print basic statsprint("Minimum distance: %f" % min(distances))print("Maximum distance: %f" % max(distances))print("Mean distance: %f" % np.mean(distances))# Compute histogramhistogram = np.histogram(distances, bins=100)# Save results to a new table nodetableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", modelNode.GetName() + " histogram")updateTableFromArray(tableNode, histogram)tableNode.GetTable().GetColumn(0).SetName("Count")tableNode.GetTable().GetColumn(1).SetName("Intensity")# Create plotplotDataNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotDataNode")plotDataNode.SetAndObserveTableNodeID(tableNode.GetID())plotDataNode.SetXColumnName("Intensity")plotDataNode.SetYColumnName("Count")plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")plotChartNode.AddAndObservePlotDataNodeID(plotDataNode.GetID())plotChartNode.SetAttribute("Type", "Bar") # delete this line for line plot# Show plot in layoutlayoutManager = slicer.app.layoutManager()layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)plotWidget = layoutManager.plotWidget(0)plotViewNode = plotWidget.mrmlPlotViewNode()plotViewNode.SetPlotChartNodeID(plotChartNode.GetID())
  File "<console>", line 1
    modelNode = getNode('VTK Output File_6-0')distanceArrayName = "Signed"# Get distances from point dataimport numpy as npdistances = vtk.util.numpy_support.vtk_to_numpy(modelNode.GetPolyData().GetPointData().GetArray(distanceArrayName))# Print basic statsprint("Minimum distance: %f" % min(distances))print("Maximum distance: %f" % max(distances))print("Mean distance: %f" % np.mean(distances))# Compute histogramhistogram = np.histogram(distances, bins=100)# Save results to a new table nodetableNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", modelNode.GetName() + " histogram")updateTableFromArray(tableNode, histogram)tableNode.GetTable().GetColumn(0).SetName("Count")tableNode.GetTable().GetColumn(1).SetName("Intensity")# Create plotplotDataNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotDataNode")plotDataNode.SetAndObserveTableNodeID(tableNode.GetID())plotDataNode.SetXColumnName("Intensity")plotDataNode.SetYColumnName("Count")plotChartNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLPlotChartNode")plotChartNode.AddAndObservePlotDataNodeID(plotDataNode.GetID())plotChartNode.SetAttribute("Type", "Bar") # delete this line for line plot# Show plot in layoutlayoutManager = slicer.app.layoutManager()layoutManager.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpPlotView)plotWidget = layoutManager.plotWidget(0)plotViewNode = plotWidget.mrmlPlotViewNode()plotViewNode.SetPlotChartNodeID(plotChartNode.GetID())
                                                              ^
SyntaxError: invalid syntax
>>>

```

---

<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: [April 18, 2021, 7:32pm UTC](https://discourse.slicer.org/t/how-to-get-the-histogram-and-area/16027/7 "2021-04-18T19:32:26Z")

</div>

It seems that you have copied the text from somewhere so that the line breaks got lost and everything got into a single line. Use a programming editor, such as Visual Studio Code, for editing code snippets - it usually handles end-of-line character differences well.
