matplotlib补丁中的颜色渐变? [英] matplotlib color gradient in patches?
本文介绍了matplotlib补丁中的颜色渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在matplotlib中创建椭圆,其填充颜色的alpha(不透明度)值取决于半径;
I'd like to create ellipses in matplotlib with a fill color that has an alpha (opacity) value that depends on the radius;
例如2D高斯.
有什么办法吗?
Is there any way to do this?
可以很容易地创建带有颜色渐变的矩形图(例如渐变facecolor matplotlib条形图和此),但我不知道该怎么做圆圈/椭圆也一样.
It is possible to create rectangular plots with color gradients easily enough (like Gradient facecolor matplotlib bar plot and this) but I can't figure out how to do the same for circles/ellipses.
推荐答案
以下是使用Alex帖子中的想法的函数示例
Here is function example using the idea from Alex's post
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-')
这是您跑步时得到的东西
And here is what you get when you run
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#
这篇关于matplotlib补丁中的颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文