To allow better handling of the text around the legend of the color bar of an exported figure in a LaTeX document, I chose to export as a PNG only the content of the figure and display manually the colorbar next to it, using pgfplots/tikz.
Some colormap are already implemented in pgfplots (see e.g. this page), but it is not the case for all of the ones that are available in ParaView.
So I wrote a small Python script allowing to convert the content of a JSON file containing the information about the colormap and convert it to a string that is to be inserted in the options of the axis
environment.
Such JSON file can be obtained with the button Save to preset
in ParaView, the resulting file looks like that.
Here is the Python code :
import json
def convert_rgb(l):
return [int(l[0]*255), int(l[1]*255), int(l[2]*255)]
def colormap_to_tikz(colormap):
output = "{" + colormap["Name"] + "}{\n"
list = colormap["RGBPoints"]
for i in range(int(len(list)/4)):
coord = list[i*4]
newcolor = convert_rgb(list[i*4+1:i*4+4])
output += f" rgb255({coord}cm)=({newcolor[0]},{newcolor[1]},{newcolor[2]});\n"
output += "}\n"
return output
f = open('rainbow.json') # path to the JSON file
j = json.load(f)
f.close()
cm = j[0] # or if the file contains the description of many color maps, change the index
result = open("rainbow.txt", "w")
result.write(colormap_to_tikz(cm))
result.close()
For instance, with the colormap Rainbow Uniform
, it will result in the following output :
{Rainbow Uniform}{
rgb255(0.0cm)=(5,97,254);
rgb255(0.023809523809523808cm)=(5,108,247);
rgb255(0.047619047619047616cm)=(5,119,239);
rgb255(0.07142857142857142cm)=(5,130,232);
rgb255(0.09523809523809523cm)=(5,139,222);
rgb255(0.11904761904761904cm)=(5,148,212);
rgb255(0.14285714285714285cm)=(5,157,202);
rgb255(0.16666666666666666cm)=(5,166,191);
rgb255(0.19047619047619047cm)=(5,174,179);
rgb255(0.21428571428571427cm)=(5,183,167);
rgb255(0.23809523809523808cm)=(5,193,153);
rgb255(0.2619047619047619cm)=(5,202,140);
rgb255(0.2857142857142857cm)=(5,211,126);
rgb255(0.30952380952380953cm)=(5,220,109);
rgb255(0.3333333333333333cm)=(5,228,91);
rgb255(0.35714285714285715cm)=(4,237,74);
rgb255(0.38095238095238093cm)=(69,242,39);
rgb255(0.40476190476190477cm)=(125,245,28);
rgb255(0.42857142857142855cm)=(164,249,11);
rgb255(0.4523809523809524cm)=(194,251,8);
rgb255(0.47619047619047616cm)=(224,252,5);
rgb255(0.5cm)=(254,254,3);
rgb255(0.5238095238095238cm)=(254,243,20);
rgb255(0.5476190476190477cm)=(254,232,37);
rgb255(0.5714285714285714cm)=(254,220,55);
rgb255(0.5952380952380952cm)=(254,208,55);
rgb255(0.6190476190476191cm)=(254,196,55);
rgb255(0.6428571428571429cm)=(254,183,55);
rgb255(0.6666666666666666cm)=(254,171,55);
rgb255(0.6904761904761905cm)=(254,159,55);
rgb255(0.7142857142857143cm)=(254,147,55);
rgb255(0.7380952380952381cm)=(254,132,55);
rgb255(0.7619047619047619cm)=(254,118,55);
rgb255(0.7857142857142857cm)=(254,104,55);
rgb255(0.8095238095238095cm)=(253,84,53);
rgb255(0.8333333333333334cm)=(251,66,48);
rgb255(0.8571428571428571cm)=(252,37,53);
rgb255(0.8809523809523809cm)=(242,29,64);
rgb255(0.9047619047619048cm)=(230,20,74);
rgb255(0.9285714285714286cm)=(218,10,84);
rgb255(0.9523809523809523cm)=(203,11,91);
rgb255(0.9761904761904762cm)=(189,11,98);
rgb255(1.0cm)=(174,12,105);
}
Finally, the LaTeX code to insert in the document is the following :
\begin{tikzpicture}
\begin{axis}[
colorbar,
% insert here the result of the Python script, for clarity I did not insert the full result here
colormap={Rainbow Uniform}{rgb255(0.0cm)=(5,97,254);rgb255(1.0cm)=(174,12,105);}
% axis equal image,
enlargelimits=false,
point meta max=10, % minimal value taken
point meta min=0, % maximal value taken
axis line style = {draw=none},
tick style = {draw=none},
xtick = \empty, ytick = \empty,
colorbar style={
ylabel = {label of the colorbar},
},
]
\addplot graphics [includegraphics cmd=\pgfimage, xmin=0, xmax=1, ymin=0, ymax=1] {path/to/the/exported/figure.png};
\end{axis}
\end{tikzpicture}
From Paraview, you should get the minimal and maximal values of the field to update point meta min
and point meta max
.
I hope this will be useful for others in the future