<jupyter_start><jupyter_text>Compute Gradients of a Field {gradients_example}============================Estimate the gradient of a scalar or vector field in a data set.The ordering for the output gradient tuple will be {du/dx, du/dy, du/dz,dv/dx, dv/dy, dv/dz, dw/dx, dw/dy, dw/dz} for an input array {u, v, w}.Showing the`pyvista.DataSetFilters.compute_derivative`{.interpreted-textrole="func"} filter.<jupyter_code># sphinx_gallery_thumbnail_number = 1
import pyvista as pv
from pyvista import examples
import numpy as np


# A vtkStructuredGrid - but could be any mesh type
mesh = examples.download_carotid()
mesh<jupyter_output><empty_output><jupyter_text>Now compute the gradients of the `vectors` vector field in the pointdata of that mesh. This is as simple as calling`pyvista.DataSetFilters.compute_derivative`{.interpreted-textrole="func"}.<jupyter_code>mesh_g = mesh.compute_derivative(scalars="vectors")
mesh_g["gradient"]<jupyter_output><empty_output><jupyter_text>::: {.note}::: {.admonition-title}Note:::You can also use`pyvista.DataSetFilters.compute_derivative`{.interpreted-textrole="func"} for computing other derivative based quantities, such asdivergence, vorticity, and Q-criterion. See function documentation foroptions.::: `mesh_g["gradient"]` is an `N` by 9 NumPy array of the gradients, so wecould make a dictionary of NumPy arrays of the gradients like:<jupyter_code>def gradients_to_dict(arr):
    """A helper method to label the gradients into a dictionary."""
    keys = np.array(["du/dx", "du/dy", "du/dz", "dv/dx", "dv/dy", "dv/dz", "dw/dx", "dw/dy", "dw/dz"])
    keys = keys.reshape((3,3))[:,:arr.shape[1]].ravel()
    return dict(zip(keys, mesh_g["gradient"].T))

gradients = gradients_to_dict(mesh_g["gradient"])
gradients<jupyter_output><empty_output><jupyter_text>And we can add all of those components as individual arrays back to themesh by:<jupyter_code>mesh_g.point_data.update(gradients)
mesh_g
keys = np.array(list(gradients.keys())).reshape(3,3)

p = pv.Plotter(shape=keys.shape)
for i in range(keys.shape[0]):
    for j in range(keys.shape[1]):
        name = keys[i,j]
        p.subplot(i,j)
        p.add_mesh(mesh_g.contour(scalars=name), scalars=name, opacity=0.75)
        p.add_mesh(mesh_g.outline(), color="k")
p.link_views()
p.view_isometric()
p.show()<jupyter_output><empty_output><jupyter_text>And there you have it, the gradients for a vector field! We could alsodo this for a scalar field like for the `scalars` field in the givendataset.<jupyter_code>mesh_g = mesh.compute_derivative(scalars="scalars")

gradients = gradients_to_dict(mesh_g["gradient"])
gradients
mesh_g.point_data.update(gradients)

keys = np.array(list(gradients.keys())).reshape(1,3)

p = pv.Plotter(shape=keys.shape)

for i in range(keys.shape[0]):
    for j in range(keys.shape[1]):
        name = keys[i,j]
        p.subplot(i,j)
        p.add_mesh(mesh_g.contour(scalars=name), scalars=name, opacity=0.75)
        p.add_mesh(mesh_g.outline(), color="k")
p.link_views()
p.view_isometric()
p.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14481856815952926, 'fraction_numerical': 0.006211180124223602, 'mean_word_length': 5.740088105726873, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '274598', 'license': 'MIT', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1073, 'n_words': 254, 'path': 'locale/examples/01-filter/gradients.ipynb', 'repo_name': 'tkoyama010/pyvista-doc-translations', 'size': '3059'}