pythonlistsavefig

loops - How to save multiple plots in python


I'm trying to save the plots I got using "el in list". With "plot.show()" I only have the last plot. I tried with another one "plot.savefig.." but it didn't work so I left them as a comment. This is the code:

plot data gas flows

list = ['N2 flow', 'O2 flow', 'CH4 flow', 'CO2 flow']

for el in list:

#using the wells names
df[['locx', 'locy', el]] = df[['locx', 'locy', el]].astype(float)
plot_data = df[['locx', 'locy', el, 'measpointname', 'measpointno']]

plot_data_11N = plot_data[plot_data.measpointno.isin(list_wells_11N)]
plot_data_11Z = plot_data[plot_data.measpointno.isin(list_wells_11Z)]
plot_data_12W = plot_data[plot_data.measpointno.isin(list_wells_12W)]
plot_data_12O = plot_data[plot_data.measpointno.isin(list_wells_12O)]

# plot data for the list
plt.rcParams["figure.figsize"] = (12,8)
plt.scatter(x=plot_data_11N['locx'], y=plot_data_11N['locy'],color='darkblue', alpha=0.5, s=plot_data_11N[el]*1000, label="11N")
plt.scatter(x=plot_data_11Z['locx'], y=plot_data_11Z['locy'],color='purple', alpha=0.5, s=plot_data_11Z[el]*1000, label="11Z")
plt.scatter(x=plot_data_12W['locx'], y=plot_data_12W['locy'],color='green', alpha=0.5, s=plot_data_12W[el]*1000, label="12W")
plt.scatter(x=plot_data_12O['locx'], y=plot_data_12O['locy'],color='red', alpha=0.5,s=plot_data_12O[el]*1000, label="12O")
plt.title(el + ' concentration flow_March 2020')
plt.xlabel('x-cordinate')
plt.ylabel('y-cordinate')
plt.legend(markerscale=0.2)
#plt.savefig('gasflows.png')
#plt.savefig(+ "{el['plot_data'].iat[0]}_plot.png")
plt.show()

Solution

  • #plt.savefig(+ "{el['plot_data'].iat[0]}_plot.png")
    

    This line should be changed. Firstly the + is used when you want to combine two strings. Then if you want to make el an iterable variable shown in the plot name, it should not be included in the ''. So you could change this line to :

        plt.savefig(el + ".png")