使用gridspec创建以三角形子图排列的标准化高斯图

时间:2016-05-26 07:40:41

标签: python matplotlib plot gaussian subplot

我正在尝试制作一个看起来有点像的三角形图:

X
o x
o o x

我有gridspec的代码:

 gs1 = gridspec.GridSpec(3,3)

'o'是椭圆图,'x'是高斯图。我的代码给了我标准化的高斯:

def gaussian(x, mu, sig_gauss):
            return 1./(sqrt(2.*pi)*sig_gauss)*np.exp(-np.power((x - mu)/sig_gauss, 2.)/2)
    sig_mean =[]
    for i in range(len(sig_gauss)):
            pairs = (1,sig_gauss[i])
            sig_mean.append(pairs)

问题是当我尝试格式化三个高斯图时,它们就在上面的排列中。我想为每个高斯分别绘制一个单独的图形,而不是全部覆盖在同一个图形上。

这是我到目前为止尝试过的事情:

axagh = []
    for mu, sig_gauss in sig_mean:
            axsbplt = plt.plot(gaussian(np.linspace(-3, 3, 120), mu, sig_gauss))
            axagh.append(axsbplt)
    ax4 = plt.subplot(gs1[0,0])
    ax4.add_figure(axagh[0])
    ax4.grid()

问题是高斯人都出现在同一个图表上,他们也不在正确的位置。

这是整个代码(练习图函数的第一部分是从前一个函数创建省略号):

def normalized_gaussian(model,x,x0,vrange, noise_type='flat',sigma=1, sigma_noise = 1,v0=80,t0=1000,beta=-2.5,delta_v=1e6,delta_t=3600*500,mu=1):
    covar = covariance_parameters(model,x,x0,vrange, noise_type= noise_type,sigma=1, sigma_noise = 1,v0=80,t0=1000,beta=-2.5,delta_v=1e6,delta_t=3600*500)
    diagonal = np.diag(covar)
    sigma_gaussian = np.sqrt(diagonal)
    gaussian = []
    #for i in range(3):
            #norm_gauss = '1/(2*np.pi*sigma_gaussian[i]**2)*np.exp(-(mu-a)**2/(2*(sigma_gaussian[i]**2)))'
            #gaussian.append(norm_gauss)
    return sigma_gaussian

这里,sigma_gaussian是一个3x1阵列。

def practice_graphs(model,x,x0,vrange, noise_type='flat',sigma=1, sigma_noise = 1,v0=80,t0=1000,beta=-2.5,delta_v=1e6,delta_t=3600*500,xlim='', ylim='', fig_axes = '',mu=1):
    plt.close()
    plt.close()
    plt.close()
    plt.close()
    a,b,rotation_angle = uncertainty_parameters(model,x,x0,vrange, noise_type=noise_type,sigma=1, sigma_noise = 1,v0=80,t0=1000,beta=-2.5,delta_v=1e6,delta_t=3600*500)
    ellipses = []
    ellipsecheck = []
    alpha = [1.52,2.48]
    color = ['b','r']  
    for i in range(3):
            for j in range(2):
                    el = patches.Ellipse(xy=(0,0), width=alpha[j]*2*a[i], height=alpha[j]*2*b[i], angle = rotation_angle[i], fill = False, edgecolor = color[j])
                    ########width and height are total (so 2a and 2b), angle in degrees
                    ellipses.append(el)
    ellipses = np.array(ellipses) #an array of all 3 ellipses' data  
    #fig1, ax = plt.subplots(2, 2, figsize=(10,7))  
    gs1 = gridspec.GridSpec(3,3)
    ax1 = plt.subplot(gs1[1,0])
    ax1.add_patch(ellipses[0])
    ax1.add_patch(ellipses[1])
    #ax1.set_xlabel(r'$\sigma_A$ (K)')
    ax1.set_ylabel(r'$\sigma_{FWHM}$ (MHz)')
    ax1.set_xticklabels([])
    ax1.grid()
    ax2= plt.subplot(gs1[-1,0])
    ax2.add_patch(ellipses[4])
    ax2.add_patch(ellipses[5])
    ax2.set_xlabel(r'$\sigma_{A}$ (K)')
    ax2.set_ylabel("$\sigma_{v0}$(MHz)")        
    ax2.grid()
    ax3 = plt.subplot(gs1[-1,-2])
    ax3.add_patch(ellipses[2])
    ax3.add_patch(ellipses[3])
    ax3.set_xlabel(r'$\sigma_{FWHM}$ (MHz)')
    ax3.set_yticklabels([])
    ax3.grid()
    ax4 = plt.subplot(gs1[0,0])
    ax5=plt.subplot(gs1[-2,-2])
    ax6 = plt.subplot(gs1[-1,-1])
    ax6.grid()
    sig_gauss=normalized_gaussian(model,x,x0,vrange, noise_type=noise_type,sigma=1, sigma_noise = 1,v0=80,t0=1000,beta=-2.5,delta_v=1e6,delta_t=3600*500,mu=1)
    def gaussian(x, mu, sig_gauss):
            return 1./(sqrt(2.*pi)*sig_gauss)*np.exp(-np.power((x - mu)/sig_gauss, 2.)/2)
    sig_mean =[]
    for i in range(len(sig_gauss)):
            pairs = (1,sig_gauss[i])
            sig_mean.append(pairs)
    axagh = []
    for mu, sig_gauss in sig_mean:
            axsbplt = plt.plot(gaussian(ax4, mu, sig_gauss))
            axagh.append(axsbplt)

    #ax4.add_figure(axagh[0])
    #ax4.grid()

    #ax4.plot(mu,f1)
    def axes_func(xlim,ylim,fig_axes):
            if fig_axes =="fixed":
                    for i in range(3):
                            ax1.set_ylim(-ylim,ylim)
                            ax1.set_xlim(-xlim,xlim)
                            ax2.set_ylim(-ylim,ylim)
                            ax2.set_xlim(-xlim,xlim)
                            ax3.set_ylim(-ylim,ylim)
                            ax3.set_xlim(-xlim,xlim)
            else:
                    #for i in range(3):
                            #gs1[i].autoscale()
                    ax1.autoscale()
                    ax2.autoscale()
                    ax3.autoscale()                        
    axes_func(xlim, ylim, fig_axes) 
    return sig_mean, axag

提前致谢!

1 个答案:

答案 0 :(得分:0)

现在你有一种令人困惑的方式来设置高斯人的情节。调用plt.plot绘制当前活动轴上的绘图,这就是为什么它们都显示在同一图表上的原因。您应该尝试更改代码以将对角线元素绘制为如下所示:

for i, (mu, sig_gauss) in enumerate(sig_mean):
        ax = plt.subplot(gs1[i,i])
        ax.plot(gaussian(np.linspace(-3, 3, 120), mu, sig_gauss))
        ax.grid()

一点解释:对于沿对角线的每个绘图,首先在gridspec上生成最佳轴,然后使用该轴绘图命令绘制高斯。