# 3D bar charts in VTK?

**URL:** https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953
**Category:** Support
**Created:** [July 14, 2022, 6:01pm UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953 "2022-07-14T18:01:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![DStranz](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DStranz](https://discourse.vtk.org/u/DStranz)
#### Post date: [July 14, 2022, 6:01pm UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953/1 "2022-07-14T18:01:14Z")

</div>

I would like to implement a 3D bar chart similar to this (from qwt3D) or what is available in matplotlib. Is there any such class in VTK? If not can I ask for some pointers about how to get started?

Thanks.

![Capture](https://discourse.vtk.org/uploads/default/original/2X/5/517e144be491cd1299bb2aa5d3a62609d8b3e544.png)

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [July 14, 2022, 9:04pm UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953/2 "2022-07-14T21:04:06Z")

</div>

You could make the bars as individual geometries and plot with pyvista like so:

```python
import numpy as np
import pyvista as pv

# Make up some data - gaussian
n = 20
x = np.linspace(-200, 200, num=n)
y = np.linspace(-200, 200, num=n)
xx, yy = np.meshgrid(x, y)
A, b = 50, 100
zz = A * np.exp(-0.5 * ((xx / b) **2.0 + (yy / b)** 2.0))

s = 3
u, v = np.meshgrid(np.arange(n)*s, np.arange(n)*s)

# Make each bar
cubes = pv.MultiBlock()
for point in np.c_[u.reshape(-1), v.reshape(-1), zz.reshape(-1)]:
    c = point
    c[-1] /=2
    bar = pv.Cube(center=c, z_length=point[-1])
    cubes.append(bar)

# Plot all bars in 3D
p = pv.Plotter(notebook=0)
p.add_mesh(cubes)
p.show_grid()
p.show()

```

 ![Screen Shot 2022-07-14 at 4.01.54 PM](https://discourse.vtk.org/uploads/default/original/2X/8/8245b195f4c2360020f0990076a857883dc50427.jpeg)

There are quite a number of ways to do this actually, such as with glyphing.

Take a look at pyvista to see more examples like this: [docs.pyvista.org](http://docs.pyvista.org)

---

<div class="post-metadata">

### Author: ![DStranz](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DStranz](https://discourse.vtk.org/u/DStranz)
#### Post date: [July 14, 2022, 9:17pm UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953/3 "2022-07-14T21:17:03Z")

</div>

Thanks. This is in essence what happens in qwt3D - the bars are “enhancements” drawn at the vertices of a 2D mesh. I’m looking for a C++ / VTK solution for use in a Qt-based C++ application, but maybe I can use the PyVista code for some ideas.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [July 19, 2022, 9:29am UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953/4 "2022-07-19T09:29:45Z")

</div>

VTK support for 3D chart is a bit lacking, but here is the class:

[https://vtk.org/doc/nightly/html/classvtkPlotSurface.html](https://vtk.org/doc/nightly/html/classvtkPlotSurface.html)

---

<div class="post-metadata">

### Author: ![DStranz](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DStranz](https://discourse.vtk.org/u/DStranz)
#### Post date: [July 19, 2022, 2:24pm UTC](https://discourse.vtk.org/t/3d-bar-charts-in-vtk/8953/5 "2022-07-19T14:24:33Z")

</div>

Thanks, Mathieu. I actually have this partly working, using the approach from pyVista - create a vtkMultiBlockDataSet and add bars created from a vtkCubeSource to it. The code is a bit too messy to post, but here’s a screenshot. The nice this is that this model also works for creating 3D scatterplots - simply replace the cubes with spheres.

![Capture](https://discourse.vtk.org/uploads/default/original/2X/2/2e038a2ee4c858cbb01da7bb378bef4bc70123c9.png)

![Capture2](https://discourse.vtk.org/uploads/default/original/2X/3/33da753bca5ad189170be98c4e3e381794cfc8b1.png)
