Python 使用gridspec创建三角形子图中的归一化高斯图
我试着画一个三角形图,看起来有点像: xPython 使用gridspec创建三角形子图中的归一化高斯图,python,matplotlib,plot,gaussian,subplot,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 =[
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()
问题是高斯分布都出现在同一张图上,而且它们也不在正确的位置
以下是全部代码(practice graph函数的第一部分是从以前的函数创建椭圆):
这里,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上生成最好的轴,然后使用该轴绘图命令绘制高斯曲线。现在,您有一种设置高斯曲线图的混乱方法。调用
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上生成最好的轴,然后使用该轴绘图命令绘制高斯曲线。您的代码毫无意义。你能粘贴完整的代码给我们工作的例子吗?我刚刚添加了完整的代码-希望这能让它更清晰,对不起,但是代码仍然没有运行。缺少函数、导入和函数调用。试试建筑。我只是想了解图形的问题,我并不关心前面的所有计算。你的代码毫无意义。你能粘贴完整的代码给我们工作的例子吗?我刚刚添加了完整的代码-希望这能让它更清晰,对不起,但是代码仍然没有运行。缺少函数、导入和函数调用。试试建筑。我只是想理解图形的问题,我并不关心前面的所有计算。