如何引用或识别在python中使用matplotlib.pyplot创建的网格中的特定网格方块?我使用了ListedColorMap来做到这一点,但我不明白如何将网格方块颜色更改为不同颜色以及ListedColormaps如何工作?
这是我的代码:
import matplotlib as mpl
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import itertools
N = 15
# making an empty data set
data = np.ones((N, N))
fig = plt.figure() # creates the figure
ax = fig.gca()
# fill in some sample data
data[6] = 1
# make color map
my_cmap = mpl.colors.ListedColormap(['g', 'b'])
ax.set_xticks(np.arange(-10, 0, 1))
ax.set_yticks(np.arange(-10, 0, 1))
for t in range(N + 1):
ax.axhline(t, lw=2, color='k', zorder=5)
ax.axvline(t, lw=2, color='k', zorder=5)
# draw the boxes
ax.imshow(data, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N],
zorder=0)
# turn off the axis labels
ax.axis('on')
plt.show()