2012-06-09 89 views

回答

7

这里是功能例如,使用这个想法从亚历克斯的帖子

import matplotlib.pyplot as plt,numpy as np 

def gauplot(centers, radiuses, xr=None, yr=None): 
     nx, ny = 1000.,1000. 
     xgrid, ygrid = np.mgrid[xr[0]:xr[1]:(xr[1]-xr[0])/nx,yr[0]:yr[1]:(yr[1]-yr[0])/ny] 
     im = xgrid*0 + np.nan 
     xs = np.array([np.nan]) 
     ys = np.array([np.nan]) 
     fis = np.concatenate((np.linspace(-np.pi,np.pi,100), [np.nan])) 
     cmap = plt.cm.gray 
     cmap.set_bad('white') 
     thresh = 3 
     for curcen,currad in zip(centers,radiuses): 
       curim=(((xgrid-curcen[0])**2+(ygrid-curcen[1])**2)**.5)/currad*thresh 
       im[curim<thresh]=np.exp(-.5*curim**2)[curim<thresh] 
       xs = np.append(xs, curcen[0] + currad * np.cos(fis)) 
       ys = np.append(ys, curcen[1] + currad * np.sin(fis)) 
     plt.imshow(im.T, cmap=cmap, extent=xr+yr) 
     plt.plot(xs, ys, 'r-') 

这里是当你运行

gauplot([(0,0), (2,3), (5,1), (6, 7), (6.1, 6.1)], [.3,. 4, .5, 1, .4], [-1,10], [-1,10]) 
      #   centers of circles   # radii of circles# 

plot

+0

我喜欢这个方法 - 我曾考虑过这个方法,但由于几个原因拒绝了它。 1.它不适用于对数日志图(不是补丁运作良好,但它们尝试)2.它需要事先知道的情节边界(尽管这可以解决)3.它很慢。不过,目前来说,我会一起滚动。 – keflavich

+0

我猜imshow的原点应该在左下角,'plt.imshow(im.T,cmap = cmap,extent = xr + yr,origin =“lower”)'。 – ImportanceOfBeingErnest

6

我不认为matplotlib目前支持补丁的渐变填充 - 请参阅this email

john>你好,我正在尝试设置一个栏(补丁系列长方形)与填充模式,而不是一个纯色。有没有一种简单的方法在matplotlib中做到这一点?
john>我正在考虑类似Qt的QBrush,它具有交叉,垂直,密集等模式。

目前还没有支持 - 对于支持这种类型的后端, 不会太难。基本上,我们需要 为它指定API,并向后端添加支持。我一直在 想要补丁(例如多边形,矩形) 添加渐变填充,这是一件好事,一次做两个。


除了使用补丁,你可以创建一个网格,计算与一个函数的颜色,然后使用imshow用插值:

# Taken from http://matplotlib.sourceforge.net/examples/pylab_examples/layer_images.html 

def func3(x,y): 
    return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2) 

# make these smaller to increase the resolution 
dx, dy = 0.05, 0.05 

x = arange(-3.0, 3.0, dx) 
y = arange(-3.0, 3.0, dy) 
X,Y = meshgrid(x, y) 

xmin, xmax, ymin, ymax = amin(x), amax(x), amin(y), amax(y) 
extent = xmin, xmax, ymin, ymax 

fig = plt.figure(frameon=False) 

Z2 = func3(X, Y) 

im2 = imshow(Z2, cmap=cm.jet, alpha=.9, interpolation='bilinear', extent=extent) 

show() 

这将导致以下(忽略格仔背景) :

http://matplotlib.sourceforge.net/_images/layer_images.png

+0

然后把白色面具的z中间用透明的补丁,你想要的,你得到了什么标记,在普通背景上获得更像渐变色块的内容。 – cphlewis