python - 将零和一的矩阵转换为彩色图
问题描述
我正在尝试将这样的数组转换为双色图 - 有零的地方:蓝色,有的地方:白色:
我试图用这段代码做到这一点:
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 没有颜色或图形的属性,这很奇怪。
解决方案
虽然这可能并不理想,但它会完成这项工作:
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()
推荐阅读
- python - 实现 filterAcceptsRows 以过滤外键
- java - 在 LibGDX(eclipse) 中逐个字符地读取文件?
- kubernetes - Volumemounts 创建为 root:artifact 尽管提供了安全上下文
- excel - 使用 Excel VBA 从 Google 或 Bloomberg 获取股票报价认为我是机器人
- powershell - 将 Python request.put 转换为 PowerShell Invoke-Restmethod
- c++ - C++ SDL2:如何将矩形渲染到多个视口中
- listview - 既然 Flutter 中的 ListView 是无状态的,那怎么可以改变呢?
- r - “'X' 和 'Y' 长度不同”RStudio 中的错误
- r - 使用此命令正确将因子转换为数字
- python - 保存时如何防止 PIL 分散 numpy 数组(图像)?