Scatterplot creation error

I am trying to create 3 subplot scatterplots, but I come up against 2 hurdles

the first is that I get the 2nd scatterplot plotted in same graph as first
(instead of a separte scatterplot with its own title)

The second is I get an error when I run the first line here
plt.figure(figsize=16,15)

plt.figure(figsize=16,15)
#subplot(nrows, ncols, index, **kwargs)

plt.subplot(2,1,1)
plt.scatter(happiness2015['HAPPINESS SCORE'],happiness2015['HEALTH LIFE EXPECTANCY'],
            color='g')
plt.xlabel('HAPPINESS SCORE')
plt.ylabel('HEALTH LIFE EXPECTANCY')

plt.subplot(2,1,1)
plt.scatter(happiness2015['HAPPINESS SCORE'],happiness2015['TRUST GOVERNMENT CORRUPTION'],
            color='b')
plt.xlabel('HAPPINESS SCORE')
plt.ylabel('TRUST GOVERNMENT CORRUPTION')

plt.suptitle('Scatterplots of Happiness Score Correlations')

I am trying to get it look similar to here to the scatterplots in this link under
Happiness Correlation Scatter Plot Code

At first glance, I can see that you’re using the same index for both plots (see above, bolded '1’s) which is why you’re getting two plots on the same figure and also why that figure is so flat: you “made room” for 2 plotting areas but only used one. Change the second subplot to:

plt.subplot(2,1,2) plt.scatter(happiness2015[‘HAPPINESS SCORE’],happiness2015[‘TRUST GOVERNMENT CORRUPTION’], color=‘b’)

You may want to change it to: plt.subplot(3, 1, *) since you mention you would like to have 3 separate scatterplots.

The reason for the error on the first line: plt.figure(figsize=16,15) is because of syntax. The figsize argument needs to be passed a tuple, not two ints separated by a comma. In other words, python thinks you’re trying to pass figsize two different values when in fact what you want to do is pass it one thing: a tuple. Hence, this should work:
plt.figure(figsize=(16,15))

Hope this helps!

1 Like

Many thanks mathmike,

Your advice worked wonders.

At first glance, I can see that you’re using the same index for both plots (see above, bolded '1’s) which is why you’re getting two plots on the same
I never understood what that index param meant until now

1 Like