Matplotlib.pyplot:如何更改颜色图中的颜色?

时间:2020-04-29 23:38:45

标签: python numpy matplotlib

我使用matplotlib.pyplot库创建了一个颜色图。我想更改数组中每个值的颜色,但是我不知道该怎么做。在下面,我提供了生成颜色图的代码,传递的数组,当前颜色图的图像以及希望颜色图的图像。

代码

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

numpyarr = np.array(arr_2d)


fig = plt.figure(figsize=(6, 3.2))

ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.imshow(numpyarr)
ax.set_aspect('equal')

cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()

numpyarr

[[32 32 32 32 46 46 46 46 46 32 32 32 32]
 [32 32 46 49 46 49 46 46 49 49 46 32 32]
 [32 46 49 49 49 49 49 49 49 49 49 46 32]
 [32 49 49 49 49 49 46 46 49 49 46 46 32]
 [46 46 49 49 49 46 46 46 49 46 49 46 46]
 [46 49 49 49 49 49 49 49 49 46 49 49 46]
 [46 46 49 49 46 49 49 46 46 46 46 46 46]
 [46 49 49 49 49 49 49 46 49 46 46 46 46]
 [46 49 49 46 49 49 49 49 46 46 46 46 49]
 [32 46 49 46 49 49 46 49 46 46 49 46 32]
 [32 32 46 49 49 49 49 49 49 49 49 32 32]
 [32 32 32 46 46 46 49 49 49 49 32 32 32]
 [32 32 32 32 32 46 46 46 32 32 32 32 32]]

当前颜色图

enter image description here

想要彩色地图

enter image description here

0 个答案:

没有答案