vedo icon indicating copy to clipboard operation
vedo copied to clipboard

plotting a matlibplot contour figure inside vedo plotter

Open Amin-Fakia opened this issue 2 years ago • 1 comments

hello marcomusy, thank you again for your time! is it possible to plot a static 2D countour figure from matplotlib inside the vedo plotter like this

image

Amin-Fakia avatar May 22 '22 17:05 Amin-Fakia

Hi, this is actually an interesting feature to be added to vedo, so it not currently implemented. You have to options at the minute:

  1. use vtk directly:

# import matplotlib
# matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
fig.add_subplot(111)

plt.plot(np.random.rand(10))
fig.tight_layout(pad=1)
fig.canvas.draw()

data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))

import vedo
pic = vedo.Picture(data)
pic.resize([400,300])
sph = vedo.Sphere().lw(1)

# pure vtk part
import vtk
mapper = vtk.vtkImageMapper()
mapper.SetInputData(pic.inputdata())
mapper.SetColorWindow(255)
mapper.SetColorLevel(127.5)
actor2d = vtk.vtkActor2D()
actor2d.SetMapper(mapper)
actor2d.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
actor2d.SetPosition(0.2, 0.1)
actor2d.GetProperty().SetDisplayLocationToBackground()
# actor2d.GetProperty().SetOpacity(0.8)
# either choose .resize() or this:
# mapper.RenderToRectangleOn()
# actor2d.SetWidth(0.4)
# actor2d.SetHeight(0.3)

vedo.show(sph, actor2d, bg='grey')

Screenshot from 2022-05-23 13-30-40 (note that you should choose a low-resolution output from matplotlib if the image is small, so you dont need to resize it)

  1. use multirenderers (but the image would move in 3d if dragged which might not be desirable). Check out examples/basic/multirenderers.py

marcomusy avatar May 23 '22 11:05 marcomusy