Sizes
Right now, the scatter plot is just a cloud of blue dots, indistinguishable from each other. Let's change this. Wouldn't it be nice if the size of the dots corresponds to the population?
To accomplish this, there is a list pop
loaded in your workspace. It contains population numbers for each country expressed in millions. You can see that this list is added to the scatter method, as the argument s
, for size.
This exercise is part of the course
Intermediate Python
Exercise instructions
- Run the script to see how the plot changes.
- Looks good, but increasing the size of the bubbles will make things stand out more.
- Import the
numpy
package asnp
. - Use
np.array()
to create a numpy array from the listpop
. Call this NumPy arraynp_pop
. - Double the values in
np_pop
setting the value ofnp_pop
equal tonp_pop * 2
. Becausenp_pop
is a NumPy array, each array element will be doubled. - Change the
s
argument insideplt.scatter()
to benp_pop
instead ofpop
.
- Import the
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import numpy as np
# Store pop as a numpy array: np_pop
# Double np_pop
# Update: set s argument to np_pop
plt.scatter(gdp_cap, life_exp, s = pop)
# Previous customizations
plt.xscale('log')
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')
plt.xticks([1000, 10000, 100000],['1k', '10k', '100k'])
# Display the plot
plt.show()