1
我想繪製一個如下所示的草圖,使用python和matplotlib。我想製作網格並不困難,但是如何以特定的方式爲特定的方塊着色?在matplotlib中繪製網格圖案
我想繪製一個如下所示的草圖,使用python和matplotlib。我想製作網格並不困難,但是如何以特定的方式爲特定的方塊着色?在matplotlib中繪製網格圖案
N = 15
# make an empty data set
data = np.ones((N, N)) * np.nan
# fill in some fake data
for j in range(3)[::-1]:
data[N//2 - j : N//2 + j +1, N//2 - j : N//2 + j +1] = j
# make a figure + axes
fig, ax = plt.subplots(1, 1, tight_layout=True)
# make color map
my_cmap = matplotlib.colors.ListedColormap(['r', 'g', 'b'])
# set the 'bad' values (nan) to be white and transparent
my_cmap.set_bad(color='w', alpha=0)
# draw the grid
for x in range(N + 1):
ax.axhline(x, lw=2, color='k', zorder=5)
ax.axvline(x, 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('off')
TNX,但我得到:__init __()得到了一個意想不到的關鍵字參數 'tight_layout' –
@AndreiBerceanu就刪除即可。它適用於更新版本的mpl – tacaswell
好吧,我更新了matplotlib到1.4,並擺脫了tight_layout錯誤。但是,現在它會發出警告:此圖包含與tight_layout不兼容的軸,因此其結果可能不正確。 –