updated_at
我希望每隔一列都为红色,其他列为蓝色,我该怎么做?
我已经尝试过了,但是却给出了错误
m=10
n=40
z=np.random.uniform(0,1,(m,n))
plt.imshow(z,cmap='Greys')
plt.colorbar()
for i in range(n):
if (np.mod(i,2)==1):
plt.imshow(z[:,i],cmap='Blues')
else :
plt.imshow(z[:,i],cmap='Reds')
答案 0 :(得分:2)
m=10
n=40
z=np.random.random(size=(m,n))
z1 = np.full(z.shape, np.nan)
z2 = np.full(z.shape, np.nan)
z1[:,::2] = z[:,::2]
z2[:,1::2] = z[:,1::2]
plt.imshow(z1,cmap='Reds')
plt.imshow(z2,cmap='Blues')
答案 1 :(得分:1)
向图中添加颜色条并不是完全有意义,但是...
我们绘制两次数组,每次用np.nan
替换某些列,这是可行的,因为Matplotlib在遇到NaN时通常不会在图表上放墨水。
In [40]: m=8
...: n=16
...: z=np.random.uniform(0,1,(m,n))
...: for i, cm in zip((0, 1), ('Blues', 'Reds')):
...: x = np.copy(z)
...: x[:,range(i, n, 2)] = np.nan
...: plt.imshow(x, cmap=cm)
...: plt.colorbar()
以上内容可以概括为n_cm
个不同的颜色图
def multi_imshow(image, cm_list, ax=None, **kwd):
nrows, ncols = image.shape
n_cm = len(cm_list)
for i, cm in enumerate(cm_list):
cols = range(i, ncols, n_cm)
x = np.full(image.shape, np.nan)
x[:,cols] = image[:,cols]
if ax:
ax.imshow( x, cmap=cm, **kwd)
else:
plt.imshow(x, cmap=cm, **kwd)
image = np.arange(16*21, dtype=float).reshape(16,21)
multi_imshow(image, ('Reds_r', 'Blues_r', 'Blues'))