上下文:Matplotlib散点图。当使用透明度(alpha
所以问题是:我如何用透明色来改变colorbar的背景色?
我正在使用Python 2.7。3和Matplotlib 1.3。1(在Ubuntu 12.04上)
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10) #for reproducability
n = 10
ticks = range(n)
data = np.hstack((np.random.rand(50,2),np.random.randint(0,n,(50,1))))
colors = plt.cm.get_cmap('jet',n)(ticks)
lcmap = plt.matplotlib.colors.ListedColormap(colors)
plt.figure(figsize=(8,4))
ax = plt.subplot(121)
plt.scatter(data[:,0],data[:,1], c=data[:,2], s=40, alpha=0.3,
edgecolor='none', cmap=lcmap)
plt.colorbar(ticks=ticks)
plt.clim(-0.5,9.5)
ax = plt.subplot(122)
plt.scatter(data[:,0],data[:,1], c=data[:,2], s=40, alpha=0.3,
edgecolor='none', cmap=lcmap)
cb = plt.colorbar(ticks=ticks)
ax.set_axis_bgcolor((0.2,0.2,0.2))
cb.ax.set_axis_bgcolor((0.2,0.2,0.2))
plt.clim(-0.5,9.5)
plt.show()
将这一行放在plt之后。clim
呼叫,
cb.patch.set_facecolor((0.2, 0.2, 0.2, 1.0))