我要根据2个变量f = 1 or 0
和x
绘制步长函数y
,以便:
0 - >透明
1 - >绿色
我想继续使用pcolor(x, y, f, cmap)
模块,因为我已经使用pcolor
模块绘制了不同的数据集,并使用不同的色彩映射。
如何获得阶梯函数图,如何在第一张图上叠加它(所以我需要透明色)?
答案 0 :(得分:0)
您可以将色彩映射设置为从绿色变为透明(请参阅here),然后将其覆盖在当前的pcolor上。
作为一个最小的例子:
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
def heaviside(x):
return .5 * (np.sign(x) + 1)
# set up a linear colormap which is green and only changes alpha
# (0,1,0,green_alpha) --> (0,1,0,0)
green_alpha = 0.1
colors = [(0,1,0,i) for i in np.linspace(green_alpha,0,2)]
gtrans = mpl.colors.LinearSegmentedColormap.from_list('mycmap', colors, N=2)
#Generate dummy data from
#http://matplotlib.org/examples/pylab_examples/pcolor_demo.html
dx, dy = 0.15, 0.05
y, x = np.mgrid[slice(-3, 3 + dy, dy),
slice(-3, 3 + dx, dx)]
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
z = z[:-1, :-1]
#Plot first colormap
fig, ax = plt.subplots(1, 1)
ax.pcolor(x, y, z, cmap='RdYlBu_r')
#Overlay rectangle using heaviside and transparency
xmin = -2.; xmax = 2.
ymin = -2.; ymax = 2.
#2D product of Heavisides defines a square
z = ( (heaviside(x+xmin) - heaviside(x+xmax))
*(heaviside(y+ymin) - heaviside(y+ymax)))
ax.pcolor(x, y, z, cmap=gtrans)
plt.show()