matplotlib:二进制热图 [英] matplotlib: binary heat plot

查看:84
本文介绍了matplotlib:二进制热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个10x10的矩阵,由10或1组成,或者由0和1组成,以列表列表的形式表示.如何使用matplotlib将矩阵表示为红色和黑色正方形的网格? (红色代表1,黑色代表0).

Let's say I have a 10x10 matrix, that is comprised or just 0s and 1s, represented as a list of lists. How could I use matplotlib to represent such a matrix as a grid of red and black squares? (red for 1, black for 0).

我已经进行了广泛的搜索,但是我能找到的最接近的是

I've search extensively, but the closest I could find was Plot a black-and-white binary map in matplotlib, but the colors are floats less than 1. Once I get 1s in the data, the plot goes awry. Can anyone help? Or point to specific matplotlib documentation that would help me overcome this?

推荐答案

您需要所谓的ListedColorMap:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

# random data
x = np.random.random_integers(0, 1, (10, 10))

fig, ax = plt.subplots()

# define the colors
cmap = mpl.colors.ListedColormap(['r', 'k'])

# create a normalize object the describes the limits of
# each color
bounds = [0., 0.5, 1.]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

# plot it
ax.imshow(x, interpolation='none', cmap=cmap, norm=norm)

这篇关于matplotlib:二进制热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆