如何使用for循环生成子批变量?

2024-06-01 02:37:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个代码,在一个图形中创建一组20个绘图(每个“轨迹”一个)。为此,我使用了命令fig,ax = plt.subplotsfor loop。然而,每次代码运行时,计算机都会返回一个5x4个空图加上每个单块的图,就像我手动运行代码20次一样

你能帮帮我吗?由于我是Python新手,如果您能简单地向我解释一下冲突是什么,我将不胜感激。代码如下所示:

fig, ax = plt.subplots(5,4,frameon=False,figsize=(22,22))
fig.tight_layout
#plt.subplots_adjust(left=0.1,bottom=0.1,right=0.90,top=0.90,wspace=0.3,hspace=1)
sns.set_style('white')
for tractName in tractCount:

    currentTract=df.query("tract == @tractName")
    coeffType1CurrentTract=coeffType1.query('tract == @tractName')
    coeffType2CurrentTract=coeffType2.query('tract == @tractName')
    
    sns.set(font_scale = 2)
    sns.set_style('white')
    ax = sns.relplot(data=currentTract, x='node', y='coeff', hue='coeffType', kind='line',palette='Blues', col = 'tract', linewidth=1) #I have changed the palette colour from 'crest' to 'Blues' as my computer didn't recognise that colour
    ax = plt.fill_between(coeffType1CurrentTract["node"], coeffType1CurrentTract['coeff']-coeffType1CurrentTract['se'], coeffType1CurrentTract['coeff']+coeffType1CurrentTract['se'], color='blue',alpha=0.2)
    ax = plt.fill_between(coeffType2CurrentTract["node"], coeffType2CurrentTract['coeff']-coeffType2CurrentTract['se'], coeffType2CurrentTract['coeff']+coeffType2CurrentTract['se'], color='blue',alpha=0.2)                
    #fig.savefig('isPremi_md.png')
plt.show()

Tags: 代码nodefigpltaxquerysetsns