2017-12-03 249 views
0

因此,我已这个矩阵,其被定义如下:设定背景色为白色包含白色

for row in range (0,squareSize): 
      for col in range (0,squareSize): 
       if new_matrix[row][col]==1: 
        new_matrix[row][col]=color_matrix[row][col] #gradient given by values in color_matrix 
       if new_matrix[row][col]==2: 
        new_matrix[row][col]=150 #color red 
       if new_matrix[row][col]==0: 
        new_matrix[row][col]=100 #color purple 

SquareSize是185,这是零的条目是点在圆内,而2的条目是圆形内部但圆外的条目。 它们都被映射到不同的颜色,并且其余部分被映射根据由下式给出颜色梯度:

cmap = mpl.cm.hsv 

使用绘制它后:

plt.matshow(new_matrix, interpolation='nearest',cmap=cmap) 

这是输出:enter image description here

不过,我想圈是白色的......这不是渐变颜色的部分:(我怎么能这样做?

回答

1

你可以设置VALU es你想出现白色到NaN。这将阻止它们被抽出,从而在这些区域中显示背景颜色。背景默认为白色,因此它们会显示为白色。

import matplotlib.pyplot as plt 
import numpy as np 

x,y=np.meshgrid(np.arange(185),np.arange(185)) 
data = np.exp(-((x-185//2)**2+(y-185//2)**2)/60.**2) 

data[(x-185//2)**2+(y-185//2)**2 > 80**2 ] = np.nan 

plt.imshow(data, cmap=plt.cm.hsv) 
plt.colorbar() 
plt.show() 

enter image description here