Python:在contourf图中设置零值颜色,其中需要对数刻度颜色条 [英] Python: setting the zero value color in a contourf plot, where log scale colorbar is required
问题描述
我正在尝试从一些数据文件制作等高线图.我遇到的问题是我希望颜色条上低于最小值的 z 值与最小值相同.当使用例如 contourf
的 extend ="both"
选项,或对颜色图使用 cmap.set_under()
.不幸的是,当使用 logscale 时,这些选项都不起作用.任何人都可以提出解决方法吗?我只想去掉下图中的白色区域:
#!/usr/bin/env python将numpy导入为np导入matplotlib.pyplot作为plt导入scipy.interpolate从matplotlib导入颜色,代码,厘米从 matplotlib.colors 导入 LogNormN = 100 #用于绘图/插值的点数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'}无花果= 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,规范= LogNorm())plt.xlabel("$ E_ {x} $")plt.ylabel("$ E/V_ {B} $")plt.colorbar()plt.show()
看来, extend
关键字不适用于对数标度是
I am trying to make a contour plot from some data files. The trouble I am having is that I want the z-values below the minimum on the color bar to be the same color as the minimum value.
This is easy when using a linear scale using e.g. the extend="both"
option for the contourf
, or using cmap.set_under()
for the colormap. Unfortunately neither of those options work when using a logscale. Can anyone suggest a workaround? I just want to get rid of the white areas in the plot below:
#!/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()
It appears that the extend
keyword not working with a log scale is a known issue with matplotlib
.
A crude workaround would be to coerce all the values into the drawn range (notice the comments on the min_drawn_value
and max_drawn_value
, the values must be inside that range):
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图中设置零值颜色,其中需要对数刻度颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!