mankoff
mankoff

Reputation: 2301

default colorbar for matplotlib

I'd like to change the global default colorbar for all graphics commands in Python matplotlib. This is similar to this question about changing the default colorbar in MATLAB. Here there is already a pythonic solution, but that solution requires making a plot in order for the change to take effect. Is there a way to set the default before the first plot is made?

Putting the following in ~/.pythonrc seems like it ought to work, but it does not:

import matplotlib.pylab as plt
plt.rcParams['image.cmap'] = plt.cm.bwr

Furthermore, I'd like to set the default to something from the colorbrewer2mpl package, not just a pre-existing matplotlib option.

Upvotes: 1

Views: 1691

Answers (2)

mankoff
mankoff

Reputation: 2301

An iPython specific solution:

Put a .py file (any name, see the README in this folder) in ~/.ipython/profile_default/startup/ with the following:

import matplotlib.pylab as plt
import brewer2mpl
cmap=brewer2mpl.get_map('RdBu', 'diverging', 7).mpl_colormap
plt.rcParams['image.cmap'] = cmap.name

Upvotes: 1

mankoff
mankoff

Reputation: 2301

One solution that does work is to customize the matplotlibrc file

Find it:

import matplotlib
matplotlib.matplotlib_fname()

Edit it:

image.cmap: bwr

However, this method only lets me select default named colorbars, not, for example, ColorBrewer colorbars available via the colorbrewer2mpl package.

Upvotes: 1

Related Questions