Using matplotlib colorbars in pgfplots

Using different programs for diagrams within one single document can be a never ending story of unmatching graphics. For example, matplotlib used a different interpolation method for colormaps than pgfplots. Disregarding alpha components, the following script produces a matching colormap. Just specify the matplotlib colormap name as command line argument and you will get a lengthy output that can be pasted to your LaTeX document. The resulting colorbars will at least look very similar:

Three sample points: different interpolation method
Three sample points: different interpolation method
pgfplots: correct interpolation
pgfplots: correct interpolation
#!/usr/bin/env python
# system modules
import sys

# third-party modules
import as cm

if len(sys.argv) != 2:
    print 'Usage: %s colormap' % sys.argv[0]

    c = cm.get_cmap(sys.argv[1])
    print 'Colormap not found'

print '\pgfplotsset{colormap={%s}{[1pt]' % sys.argv[1]
for i in range(101):
    print 'rgb(%spt)=%s;' % (i, c(i/100.0)[0:3])
print '}}'

Leave a comment

Your email address will not be published.