Get startedGet started for free

GDP vs. life expectancy legend

The United Nations has contacted you to help them understand and visualize their data. They have been playing with various visualization tools but can't seem to find the design that they want.

They want to understand the relationship (if it exists) between GDP and life expectancy and have gathered data on over 200 countries to analyze. However, their initial efforts have confused stakeholders, and they need a clear legend below the plot to help viewers understand it.

Your task is to create a scatterplot using the provided life_gdp DataFrame and style and position the legend as requested.

This exercise is part of the course

Introduction to Data Visualization with Plotly in Python

View Course

Exercise instructions

  • Create a scatterplot using the life_gdp DataFrame, setting the x-axis to be Life expectancy, the y-axis to be GDP Per Capita.
  • Create a legend dictionary positioned 20% along the x-axis and 95% up the y-axis.
  • Update the layout of your scatterplot to show the legend you just created!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create the scatterplot
fig = px.scatter(
        data_frame=____, 
        x="____", 
        y="____",
  		color="Continent")

# Create legend dictionary
my_legend = dict(x=0.2, y=____)

# Update the figure
fig.update_layout(showlegend=____, legend=my_legend)

# Show the plot
fig.show()
Edit and Run Code