首页 > 解决方案 > 将零和一的矩阵转换为彩色图

问题描述

我正在尝试将这样的数组转换为双色图 - 有零的地方:蓝色,有的地方:白色:

数组编号

数组作为图像

我试图用这段代码做到这一点:

import numpy as np
import matplotlib as plt

def plot_islands(matrix):
array=np.matrix
colors=np.where(array==1,plt.figure(figsize=(1,1), width=1, height=1, color='b'),plt.figure((1,1), width=1, height=1, color='b'))
plt.show()

python 抱怨 matplotlib 没有颜色或图形的属性,这很奇怪。

标签: pythonnumpymatplotlib

解决方案


虽然这可能并不理想,但它会完成这项工作:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np


def plot_islands(matrix):
    cmap = LinearSegmentedColormap.from_list('my_cmap', ['darkblue', 'white'])
    plt.imshow(X=matrix, cmap=cmap)
    plt.show()

推荐阅读