Python对数颜色图/调色板

时间:2019-06-05 09:22:41

标签: python matplotlib colors plotly seaborn

我想使用一个色图,其中高值的色差很高,而其他值的色差不大。我认为对数颜色图将是最好的方法。我想绘制一个热图,与是否在matplotlib,seaborn,plotly中无关。

像这样创建对数颜色图的示例总是创建一个网格并为X和Y获取一些东西,但是我只有一个2d数组(代表一个图像)。 :https://matplotlib.org/3.1.0/gallery/images_contours_and_fields/pcolor_demo.html#sphx-glr-gallery-images-contours-and-fields-pcolor-demo-py

N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]

# A low hump with a spike coming out.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
Z1 = np.exp(-(X)**2 - (Y)**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2

fig, (ax0, ax1) = plt.subplots(2, 1)

c = ax0.pcolor(X, Y, Z,
               norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')
fig.colorbar(c, ax=ax0)

c = ax1.pcolor(X, Y, Z, cmap='PuBu_r')
fig.colorbar(c, ax=ax1)

plt.show()

也与此帖子类似:Second example for logaithmic colormap

例如,如果我只有一个像a=np.array([[0,2,3],[4,5,6],[7,8,9]])这样的image / 2d数组,那怎么办?

1 个答案:

答案 0 :(得分:2)

def xml = readFile file: "${strNuspec}", encoding: "${encoding}"

def getXmlInnerText = { attribute ->
  def matcher = xml =~ "<$attribute>(.+)</$attribute>"
  matcher ? matcher[0][1] : null
}

def version = getXmlInnerText("version")
println version

enter image description here

如果您的值并非严格为正,则需要先对其进行屏蔽,在此将其替换为nan:

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
plt.imshow(a, norm=matplotlib.colors.LogNorm(vmin=a.min(), vmax=a.max()))
plt.colorbar()

enter image description here