首页 > 解决方案 > 如何使用 for 循环绘制变量?

问题描述

我正在尝试编写一个代码,在一个图中创建一组 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()

标签: python

解决方案


推荐阅读