# Bar charts with categorical data

**URL:** <https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615>\
**Category:** Development\
**Created:** [October 9, 2022, 2:42pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615 "2022-10-09T14:42:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 9, 2022, 2:42pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/1 "2022-10-09T14:42:54Z")

</div>

vtkMRMLChartNode is no longer supported in Slicer 5 - how are we supposed to realize somting like

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

as it was (is) described in

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Charts](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Charts)

?  
Thank you.

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 9, 2022, 4:53pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/2 "2022-10-09T16:53:31Z")

</div>

[Here’s how I approached plotting recenetly.](https://gist.github.com/ebrahimebrahim/8b899552da072fdd22727cfbf0e3a804)

In the last line of that gist you can set `plot_type='bar'` to get a bar chart.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 9, 2022, 5:22pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/3 "2022-10-09T17:22:10Z")

</div>

Thank you.  
`How would you set the x axis labels (“tissue”, “bone” etc) in your

```auto
# Use set_plot_data to update what is shown in the plot view:
slicer_plot_data.set_plot_data(np.random.normal(size=(10,2)))

```

line? Could you even set the bar colors?

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [October 9, 2022, 5:32pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/4 "2022-10-09T17:32:35Z")

</div>

There is a `labels` argument in `set_plot_data` but it doesn’t appear to show labels at the bottom, but rather only when you mouse over the bars.

I don’t know if there is a way to set colors of individual bars they way you show above.

If you see a way to do either of these things then please share 🙂  
It may be a limitation in `vtkMRMLPlotChartNode`’s bar plotting functionality.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 9, 2022, 10:24pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/5 "2022-10-09T22:24:24Z")

</div>

There’s a lot of [underlying functionality](https://www.na-mic.org/w/img_auth.php/1/18/NA-MIC-VTK-Charts-2011.pdf) and it could be extended and exposed in Slicer.

If you are feeling adventurous [this pyqtgraph example](https://discourse.slicer.org/t/pythonqt-properties-shadowing-methods/16992/9) apparently works.

But maybe the safest and easiest would be to [use maplotlib](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#using-matplotlib) maybe with [seaborn](https://seaborn.pydata.org/generated/seaborn.barplot.html). It’s not fully integrated with slicer though, so some extra work would be required to make it show up in a layout.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 10, 2022, 4:32pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/6 "2022-10-10T16:32:14Z")

</div>

Thanks, Steve.  
Very interesting links.  
This is to make CIP\_ParenchymaAnalysis fully compatible again …  
Would it not be better to have a simple slicer.util based solution for such a simple and frequently used graph if vtkMRMLChartNode is suddenly not supported any longer? Even analyzing this has cost hours.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 10, 2022, 5:10pm UTC](https://discourse.slicer.org/t/bar-charts-with-categorical-data/25615/7 "2022-10-10T17:10:05Z")

</div>

Yes, I agree it would be good to have a solid answer to make this easy. Making these scientific visualizations is a complicated topic and all the options are incomplete or have drawbacks of one kind or another. My approach would be to make it easy to use one of the mainstream packages like seaborn for this purpose, although extending the VTK-based approach is also an option.
