Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standalone colorbar (matplotlib)

I'm rendering some graphics in python with matplotlib, and will include them into a LaTeX paper (using LaTex's nice tabular alignment instead of fiddling with matplotlib's ImageGrid, etc.). I would like to create and save a standalone colorbar with savefig, without needing to use imshow.

(the vlim, vmax parameters, as well as the cmap could be provided explicitly)

The only way I could find was quite complicated and (from what I understand) draws a hard-coded rectangle onto the canvas: http://matplotlib.org/examples/api/colorbar_only.html

Is there an elegant way to create a standalone colorbar with matplotlib?

like image 676
user Avatar asked May 16 '13 18:05

user


People also ask

What is a mappable Matplotlib?

A colorbar needs a "mappable" ( matplotlib. cm. ScalarMappable ) object (typically, an image) which indicates the colormap and the norm to be used. In order to create a colorbar without an attached image, one can instead use a ScalarMappable with no associated data.

How do I change the color of a bar in Python?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.


1 Answers

You can create some dummy image and then hide it's axe. Draw your colorbar in a customize Axes.

import pylab as pl import numpy as np  a = np.array([[0,1]]) pl.figure(figsize=(9, 1.5)) img = pl.imshow(a, cmap="Blues") pl.gca().set_visible(False) cax = pl.axes([0.1, 0.2, 0.8, 0.6]) pl.colorbar(orientation="h", cax=cax) pl.savefig("colorbar.pdf") 

the result:

enter image description here

like image 123
HYRY Avatar answered Oct 11 '22 19:10

HYRY