# How to export ParaView colormap into a format that could be read by matplotlib?

**URL:** https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394
**Category:** ParaView Support
**Created:** [August 9, 2019, 4:05pm UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394 "2019-08-09T16:05:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![myousefi2016](https://discourse.paraview.org/user_avatar/discourse.paraview.org/myousefi2016/32/2457_2.png) [@myousefi2016](https://discourse.paraview.org/u/myousefi2016)
#### Post date: [August 9, 2019, 4:05pm UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394/1 "2019-08-09T16:05:55Z")

</div>

I’m wondering is it possible to export ParaView colormap in RGB format to be able to read it in matplotlib? I know that ParaView could export colormaps in .json format but I don’t know how to convert it in a suitable format to be able to read it by using `matplotlib.colors.ListedColormap` function. Any idea or help is appreciated.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [August 9, 2019, 6:17pm UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394/2 "2019-08-09T18:17:44Z")

</div>

Sure, here is a little function that can export the color map in a table:

```python
def ExportPreset(lutName):
  from paraview import servermanager
  presets = paraview.servermanager.vtkSMTransferFunctionPresets()

  lut = GetColorTransferFunction('dummy')
  lut.ApplyPreset(lutName)

  import vtk
  helper = vtk.vtkUnsignedCharArray()

  dctf = lut.GetClientSideObject()
  dataRange = dctf.GetRange()

  for i in range(0, 256):
    x = (i/255.0) * (dataRange[1]-dataRange[0]) + dataRange[0]
    helper.SetVoidArray(dctf.MapValue(x), 3, 1)
    r = helper.GetValue(0)
    g = helper.GetValue(1)
    b = helper.GetValue(2)
    print("%d %d %d" % (r, g, b))

```

`lutName` is a string with the name of the colormap you’d like to export.

Evaluate and call this function in the **Python Shell** (View -\> Python Shell). You can adapt the script to produce the type of array needed for the `matplotlib` function.

---

<div class="post-metadata">

### Author: ![myousefi2016](https://discourse.paraview.org/user_avatar/discourse.paraview.org/myousefi2016/32/2457_2.png) [@myousefi2016](https://discourse.paraview.org/u/myousefi2016)
#### Post date: [August 9, 2019, 7:29pm UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394/3 "2019-08-09T19:29:07Z")

</div>

Thanks Cory, it worked smoothly!

---

<div class="post-metadata">

### Author: ![steve\_r](https://discourse.paraview.org/user_avatar/discourse.paraview.org/steve_r/32/8305_2.png) [@steve\_r](https://discourse.paraview.org/u/steve_r)
#### Post date: [May 23, 2022, 3:26pm UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394/4 "2022-05-23T15:26:26Z")

</div>

I’m trying to export the opacities but have some trouble, they always return as fully opaque for me. Came across this old response. Should this also return the opacities in the MapValue() function? Or do I need to call the GetOpacityTransferFunction() and do similar operations in a second loop? Thx

---

<div class="post-metadata">

### Author: ![dutta-alankar](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dutta-alankar/32/13310_2.png) [@dutta-alankar](https://discourse.paraview.org/u/dutta-alankar)
#### Post date: [March 9, 2026, 9:43am UTC](https://discourse.paraview.org/t/how-to-export-paraview-colormap-into-a-format-that-could-be-read-by-matplotlib/2394/5 "2026-03-09T09:43:10Z")

</div>

This might be an old post but can be useful to someone. The following python script can export any paraview colormap to be exported and independently used as a colormap in matplotlib python.

```auto
import json
import vtk
from paraview import servermanager
from paraview.simple import GetColorTransferFunction

def ExportToJSON(lutName):
    filename=f"{lutName.lower()}.json"
    lut = GetColorTransferFunction(lutName)
    if not lut.ApplyPreset(lutName, True):
        print(f"Preset {lutName} not found!")
        return

    dctf = lut.GetClientSideObject()
    dataRange = dctf.GetRange()
    
    rgb_data = []
    helper = vtk.vtkUnsignedCharArray()

    for i in range(256):
        x = (i / 255.0) * (dataRange[1] - dataRange[0]) + dataRange[0]
        helper.SetVoidArray(dctf.MapValue(x), 3, 1)
        
        # Normalize to [0, 1] for Matplotlib compatibility
        r = helper.GetValue(0) / 255.0
        g = helper.GetValue(1) / 255.0
        b = helper.GetValue(2) / 255.0
        rgb_data.append([r, g, b])

    with open(filename, 'w') as f:
        json.dump(rgb_data, f)
    
    print(f"Colormap exported successfully to {filename}")

if __name__ == " __main__":
    ExportToJSON('Spectral_lowBlue')

    import json
    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    import numpy as np

    def load_pv_colormap(json_file):
        with open(json_file, 'r') as f:
            rgb_list = json.load(f)
        return ListedColormap(rgb_list)

    # Usage
    my_cmap = load_pv_colormap('spectral_lowblue.json')

    # Quick test visualization
    data = np.random.rand(10, 10)
    plt.imshow(data, cmap=my_cmap)
    plt.colorbar()
    plt.savefig("test-colormap.png")

```
