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?
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With