Get startedGet started for free

GDP vs. life expectancy legend

You have been contacted by the United Nations to help them understand and visualize their data. They have been playing with various visualization tools, but just 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 been confusing to stakeholders and they need a clear legend positioned 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 and colors based on Continent.
  • Create a legend dictionary that is positioned 20% along the x-axis and 95% up the y-axis.
  • Format the legend box to have a background color of the legend to be RGB code (60, 240, 201) and border width of 5.
  • 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='____')

# Create the legend
my_legend = {'x': ____, 'y': ____, 
            'bgcolor': ____, 'borderwidth': ____}

# Update the figure
fig.update_layout({'showlegend': ____, 'legend': ____})

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