pythonpandasmatplotlibseabornsavefig

Dynamic naming using savefig


I have a df with some number of columns, and I'm creating bivariate plots using a loop like so:

for col in df.columns[2:len(df.columns)]:
    sns.factorplot(x='xvar', y=col,  data=df)
    sns.despine(offset=10, trim=True)

    plt.show()
    plt.clf()

plt.close()

Instead of displaying all the plots I'd like to save them with the naming convention [xaxis]_y[axis].png. However, I'm not sure how to pass more than 1 item through plt.savefig() where one of the items will change every time. I've seen a few answers that rely on lists (1, 2), but I'm hoping there's a way to achieve the same results without having to create lists to pass through.


Solution

  • plt.savefig('var1' + '_' + col' + '.png')
    

    Achieved the desired result