Editor

Choose the Right Chart Type

Advanced · Write the code · from Data Visualization: Choose the Right Chart and Tell a Story

From the lesson: Data Visualization: Choose the Right Chart and Tell a Story

A chart that looks pretty but tells the wrong story is worse than no chart at all. In 1986, NASA engineers showed managers a chart arguing that cold temperatures could damage the Space Shuttle Challenger's O-ring seals. The chart was so poorly designed that the managers couldn't see the pattern.

Read the lesson

Write a function choose_chart(data_description) that takes a string describing the data and returns the best chart type. Handle these cases: if the description contains "over time" or "trend", return 'line'. If it contains "compare" or "categories", return 'bar'. If it contains "relationship" or "correlation", return 'scatter'. If it contains "distribution", return 'histogram'. If it contains "proportion" or "percentage", return 'bar' (not pie -- bar is almost always better). Otherwise return 'bar' as the safe default. Test with the provided descriptions.

def choose_chart(data_description):
desc = data_description.lower()
# Your logic here
pass
# Test cases
print(choose_chart('Sales over time for 2024'))
print(choose_chart('Compare revenue across departments'))
print(choose_chart('Relationship between age and income'))
print(choose_chart('Distribution of test scores'))
print(choose_chart('Proportion of budget by category'))

More Data Visualization exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz