I'm trying to plot these scatterplots in a figure with a loop. I'm using matplotlib (plt) and seaborn (sns).
plt.subplot(2, 3, 1)
sns.scatterplot(data = chile, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Chile')
plt.subplot(2, 3, 2)
sns.scatterplot(data = china, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('China')
plt.subplot(2, 3, 3)
sns.scatterplot(data = germany, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Germany')
plt.subplot(2, 3, 4)
sns.scatterplot(data = mexico, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Mexico')
plt.subplot(2, 3, 5)
sns.scatterplot(data = usa, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('United States of America')
plt.subplot(2, 3, 6)
sns.scatterplot(data = zimbabwe, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Zimbabwe')
plt.subplots_adjust(wspace = 0.5, hspace = 0.5)
plt.show()
plt.clf()
My figure
I tried looping through a list of my data, but it just made a new figure and plotted the graph in the first position.
countries = [chile, china, germany, mexico, usa, zimbabwe]
for country in countries:
plot_number = 1
plt.subplot(2, 3, plot_number)
sns.scatterplot(data = country, x = 'Year', y = 'Life expectancy at birth (years)')
plot_number += 1
plt.show()
plt.clf()
I'm trying to plot these scatterplots in a figure with a loop. I'm using matplotlib (plt) and seaborn (sns).
plt.subplot(2, 3, 1)
sns.scatterplot(data = chile, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Chile')
plt.subplot(2, 3, 2)
sns.scatterplot(data = china, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('China')
plt.subplot(2, 3, 3)
sns.scatterplot(data = germany, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Germany')
plt.subplot(2, 3, 4)
sns.scatterplot(data = mexico, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Mexico')
plt.subplot(2, 3, 5)
sns.scatterplot(data = usa, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('United States of America')
plt.subplot(2, 3, 6)
sns.scatterplot(data = zimbabwe, x = 'Year', y = 'Life expectancy at birth (years)')
plt.title('Zimbabwe')
plt.subplots_adjust(wspace = 0.5, hspace = 0.5)
plt.show()
plt.clf()
My figure
I tried looping through a list of my data, but it just made a new figure and plotted the graph in the first position.
countries = [chile, china, germany, mexico, usa, zimbabwe]
for country in countries:
plot_number = 1
plt.subplot(2, 3, plot_number)
sns.scatterplot(data = country, x = 'Year', y = 'Life expectancy at birth (years)')
plot_number += 1
plt.show()
plt.clf()
Share
Improve this question
asked Mar 19 at 20:58
Martin EspinoMartin Espino
31 bronze badge
0
3 Answers
Reset to default 2On each iteration of your loop, plot_number
is set to 1, so incrementing it isn't going to do anything. Try moving plot_number = 1
to outside of the for loop:
countries = [chile, china, germany, mexico, usa, zimbabwe]
plot_number = 1
for country in countries:
plt.subplot(2, 3, plot_number)
sns.scatterplot(data = country, x = 'Year', y = 'Life expectancy at birth (years)')
plot_number += 1
# needs to be outside the loop otherwise you'll create 6 different figures
plt.show()
# needs to be outside the loop otherwise you'll clear the figure each time
plt.clf()
Don't manually increment plot_number
. Let enumerate
do the work for you - it's the most pythonic way to track indices and values simultaneously.
Move the show
and clf
calls out of the loop as well, for the same reasons blake and Cesar outlined.
countries = [chile, china, germany, mexico, usa, zimbabwe]
for plot_number, country in enumerate(countries, 1):
plt.subplot(2, 3, plot_number)
sns.scatterplot(data = country, x = 'Year', y = 'Life expectancy at birth (years)')
plt.show()
plt.clf()
Maybe if you put the (plt.show() and plt.clf() ) out of the for loop. Good luck.