vedo
vedo copied to clipboard
plotting a matlibplot contour figure inside vedo plotter
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
Hi, this is actually an interesting feature to be added to vedo
, so it not currently implemented.
You have to options at the minute:
- 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')
(note that you should choose a low-resolution output from matplotlib if the image is small, so you dont need to resize it)
- use multirenderers (but the image would move in 3d if dragged which might not be desirable). Check out
examples/basic/multirenderers.py