1
我已經繪製的階梯函數f = 1 or 0
取決於2個變量x
和y
,使得:爲階躍函數離散顏色表
0 - >透明
1 - >綠色
I」 d喜歡繼續使用pcolor(x, y, f, cmap)
模塊,因爲我已經使用pcolor
模塊繪製不同的數據集,並使用不同的顏色映射表。
如何獲得階梯函數圖,以及如何在第一個圖上疊加它(因此我需要透明顏色)?
我已經繪製的階梯函數f = 1 or 0
取決於2個變量x
和y
,使得:爲階躍函數離散顏色表
0 - >透明
1 - >綠色
I」 d喜歡繼續使用pcolor(x, y, f, cmap)
模塊,因爲我已經使用pcolor
模塊繪製不同的數據集,並使用不同的顏色映射表。
如何獲得階梯函數圖,以及如何在第一個圖上疊加它(因此我需要透明顏色)?
您可以設置顏色圖從綠色變爲透明(請參見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()