Plot data from a .pvd file in PythonView

Hello, following this tutorial I managed to create a histogram of the datapoints from a Paraview file with PythonView. Now I would like to consider the Paraview file attached, and plot it in PythonView. Note that the .pvd file attached represents a function of two variables, say, f(x,y).

Now I execute the following script with PythonView

from paraview import python_view
import numpy as np
import matplotlib.pyplot as plt

def render(view, width, height):
    figure = python_view.matplotlib_figure(width, height)
    ax = figure.add_subplot(1,1,1)

    ax.set_title('My beautiful plot')
    ax.set_xlabel('x axis')
    ax.set_ylabel('y axis')

    data_object = view.GetVisibleDataObjectForRendering(0)
    x = data_object.GetPointData().GetArray('f_24')
    from paraview.numpy_support import vtk_to_numpy
    np = vtk_to_numpy(x)

    
    return python_view.figure_to_image(figure)


where I would like to store in np the values of f(x,y). However, when I run this Python code in paraview I get the error message

Traceback (most recent call last):
  File "/Applications/ParaView-5.12.0.app/Contents/Python/paraview/python_view.py", line 156, in call_render
    image = render_function(view, width, height)
  File "Script", line 16, in render
  File "/Applications/ParaView-5.12.0.app/Contents/Python/vtkmodules/util/numpy_support.py", line 215, in vtk_to_numpy
    typ = vtk_array.GetDataType()
AttributeError: 'NoneType' object has no attribute 'GetDataType'

What am I doing wrong? I would like to get the values of f(x,y) stored into an array, and similarly for the values of x, which I would like to store in a second array, and for the values of y, which I would like to store in a third array.

Thank you

solution.pvd (170 Bytes)
solution000000.vtu (671.3 KB)

Hi @legno,

You’ll need to define a setup_data(view) function to enable transfer of the arrays from the server to the view:

def setup_data(view):
    view.EnableAllAttributeArrays()

Add that to your script and it should work fine.

By the way, that’s covered in the tutorial you shared at https://youtu.be/OeG0XPv3eZw?t=814