Python:在contourf图中设置零值颜色,其中需要日志比例颜色条

我想从某些数据文件中制作等高线图。我遇到的麻烦是我想要颜色栏上的最小值以下的z值与最小值的颜色相同。 当使用例如线性比例尺时,这很容易。 contourfextend="both"选项,或者使用cmap.set_under()作为颜色映射。不幸的是,在使用logscale时,这些选项都不起作用。任何人都可以提出一个解决方法?我只是想摆脱在下面的图中的白色区域:Python:在contourf图中设置零值颜色,其中需要日志比例颜色条

#!/usr/bin/env python 

import numpy as np

import matplotlib.pyplot as plt

import scipy.interpolate

from matplotlib import colors, ticker, cm

from matplotlib.colors import LogNorm

N = 100 #number of points for plotting/interpolation

y, x, z = np.genfromtxt(r'40Ca_208Pb_39K_Ex_115deg.dat', unpack=True)

xi = np.linspace(x.min(), x.max(), N)

yi = np.linspace(y.min(), y.max(), N)

zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='linear')

hfont = {'fontname':'Palatino'}

fig = plt.figure(facecolor="white")

zi = np.ma.masked_less(zi, 1e-7)

plt.contourf(xi, yi, zi,levels=[1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1],cmap=plt.cm.jet,norm = LogNorm())

plt.xlabel("$E_{x}$")

plt.ylabel("$E/V_{B}$")

plt.colorbar()

plt.show()

回答:

看来,extend关键字不是对数刻度工作是known issue与matplotlib

粗的解决方法可能是将所有的值对象转换成绘制的范围(注意在min_drawn_valuemax_drawn_value的意见,值必须在该范围内):

import numpy as np 

import matplotlib.pyplot as plt

from matplotlib.colors import LogNorm

N = 100 # number of points for plotting/interpolation

min_exp = -8

max_exp = -2

min_drawn_value = 1.000001 * 10.**min_exp # above 10.**min_exp

max_drawn_value = 0.999999 * 10.**max_exp # below 10.**max_exp

xi = np.linspace(0, 1, N)

yi = np.linspace(0, 1, N)

zi = np.random.rand(N, N) *\

10. ** np.random.randint(min_exp - 1, max_exp + 2, (N, N))

zi = np.sort(zi.flatten()).reshape(N,N)

# Coerce values outside of colorbar range to lie within

zi_masked = np.where(zi < 10.**min_exp, min_drawn_value, zi)

zi_masked = np.where(zi_masked > 10.**max_exp, max_drawn_value, zi_masked)

fig, (ax,ax2) = plt.subplots(ncols=2)

c1 = ax.contourf(xi, yi, zi, levels=10.**np.arange(min_exp, max_exp+1),

cmap=plt.cm.jet, norm=LogNorm())

c2 = ax2.contourf(xi, yi, zi_masked, levels=10.**np.arange(min_exp, max_exp+1),

cmap=plt.cm.jet, norm=LogNorm())

ax.set_title("direct plot of array")

ax2.set_title("coerce outlier values")

fig.colorbar(c1, ax=ax)

fig.colorbar(c2, ax=ax2)

fig.subplots_adjust(wspace=0.3)

plt.show()

以上是 Python:在contourf图中设置零值颜色,其中需要日志比例颜色条 的全部内容, 来源链接: utcz.com/qa/259798.html

回到顶部