2020-12-26 15:47

Opacity by scalar array or custom transfer function

These changes enable a user to specify all kinds of options for the opacity:

  • a scalar array on the mesh be used for opacity mapping to enable us to see two arrays at once
  • predefined transfer functions by string name
  • custom built transfer functions
  • single value for global opacity


Maybe you have an uncertainty array - these changes make it so that you could make regions with high values of uncertainty be transparent. Or maybe you have a mesh with a modeled scalar array on another array of your modeling variance. This would allow you to control the opacity of the mesh by the variance.


Here I have a mesh with a modelled scalar array from Kriging and an associated kriging variance array which I'd like to use to make my mesh transparent:

temp_grid = pv.read('temp_grid.vtk')
HeaderData ArraysUnstructuredGridInformationN Cells2841790N Points551724X Bounds3.299e+05, 3.442e+05Y Bounds4.253e+06, 4.271e+06Z Bounds-2.700e+03, 2.300e+03N Arrays2NameFieldTypeN CompMinMaxTemperaturePointsfloat6416.729e+002.738e+02Temperature_varPointsfloat6412.214e+021.107e+04
# Make some contours
cntrs = temp_grid.contour(10, scalars='Temperature')

p = pv.Plotter(notebook=0)
p.add_mesh(cntrs, scalars='Temperature', 
           flip_opacity=True, cmap='bwr')

Screen Shot 2019-07-07 at 4 09 40 PM

Which looks a bit more informative than just the scalar array alone:

Screen Shot 2019-07-07 at 4 05 30 PM


  • [x] fix issue where the scalar bar is not appearing
  • [x] fix the issue where the added __custom_rgba array is set as active and would be plotted wrong on the second iteration.
  • [x] Add tests
  • [x] Add the ability to use user-specified mappings/transfer functions per #301
  • [x] throw error if scalars and opacity arrays are on different fields (cell/point)
  • [x] refactor and implement for add_volume as well (add_volume doesn't currently support RGBA plotting - this will come later)
  • [x] add examples!


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39611008 weixin_39611008 4月前

    Might consider adding “Normals”.

    点赞 评论 复制链接分享
  • weixin_39949894 weixin_39949894 4月前

    Example using custom-defined transfer function:

    import pyvista as pv
    from pyvista import examples
    mesh = examples.load_uniform()
    opacity = [0,0.2,0.9,0.2,0]
    p = pv.Plotter()
    p.add_mesh(mesh, scalars='Spatial Point Data', opacity=opacity, 


    点赞 评论 复制链接分享
  • weixin_39949894 weixin_39949894 4月前

    Note the new exclusion list for when accessing the active scalar. We might want to add more names to this like 'vtkOriginalPointIds' to mitigate users experiencing #303

    点赞 评论 复制链接分享
  • weixin_39640265 weixin_39640265 4月前

    looks cool!!

    点赞 评论 复制链接分享
  • weixin_39995439 weixin_39995439 4月前

    Codecov Report

    Merging #297 into master will increase coverage by 0.07%. The diff coverage is 90.38%.

    @@            Coverage Diff             @@
    ##           master     #297      +/-   ##
    + Coverage   83.99%   84.07%   +0.07%     
      Files          30       30              
      Lines        6847     6906      +59     
    + Hits         5751     5806      +55     
    - Misses       1096     1100       +4
    点赞 评论 复制链接分享