3

The colorbar takes the same transparency as the contourf-plot which I've placed over an image.

I can achieve a non-transparent colorbar by adding a fake contourf plot without transparency at the lowest "zorder"-location, but I guess there is a proper way?

This gives me a semi-transparent colorbar:

cs = m.contourf(xv,yv,zi,zorder=4,alpha=0.7,origin="lower")
cbar = m.colorbar(cs,location='right',pad="5%")
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
Amelse Etomer
  • 1,253
  • 10
  • 28

1 Answers1

8

You can do it, but there's no particular method devoted to just that. (Perhaps there should be.) Instead, you can manually modify the alpha value of cbar.solids.

For example, let's demonstrate the general problem:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(data, cmap='gist_earth', alpha=0.5)

cbar = fig.colorbar(im)

plt.show()

enter image description here

And then if we change the transparency of cbar.solids:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(data, cmap='gist_earth', alpha=0.5)

cbar = fig.colorbar(im)
cbar.solids.set(alpha=1)

plt.show()

enter image description here

On a side note, if you were working with a transparent contour instead of contourf, you might want to modify the alpha value of all of cbar.lines, as well.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463