使用 gridspec 创建排列在三角形子图中的归一化高斯图

Creating normalized gaussian graphs arranged in triangular subplots using gridspec

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

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

提前致谢!

现在您有一种令人困惑的设置高斯图的方法。调用 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 上生成最好的轴,然后使用该轴绘图命令绘制高斯。