Don't use a legend (do this instead)
You're now more than 9000 people reading this newsletter ๐ฑ. Thank you so much! ๐๐๐
If you want to share it with a friend, I just put together a (for now, ugly) homepage with all past issues.
As always, today's tip is short but powerful. Let's talk aboutโฆ
The problem with legends
Most dataviz libraries make it easy to add legends. That's great! Sometimes, they're essential for understanding a chart.
But most of the time? Legends are a hassle.
Take this stacked area chart, for example:
Stacked area chart made with Matplotlib in my Matplotlib Journey project.
Reading it means constantly jumping back and forth between the chart and the legend. What does this color mean? Check the legend. What about this one? Check again. And again. ๐ณ
That's a lot of unnecessary cognitive load. Instead of absorbing the story, your audience will struggle just to decode the chart. You'll lose their attention.
So here's my advice: If you can remove the legend, remove it.
Solution 1: Direct labeling
Instead of using a separate legend, label the groups directly on the chart.
Line charts / Stacked area charts? Place the labels near the end of the lines. Scatterplots? Find a good spot near the group's center. Streamgraphs? Position labels inside the flow where they fit naturally.
Here is an example we've built for the Python Graph Gallery:
You see a group, and know instantly what it is! This takes extra effort, but trust me: your readers will thank you.
Technical tip:
In R, the ggrepel package helps position labels without overlap. In Python, check out drawarrow for easy annotation with arrows.
Solution 2: Color in the title
That's another solution: use colors in the title to highlight key categories.
This way, you deliver the main message and introduce the groups in one go.
Cedric Scherer uses this technique very often. Here is an example with a dumbbell chart from the R graph gallery:
Takeaway
Whenever possible, ditch the legend to make your chart easier to read.
Still skeptical? Check out Dataviz-inspiration.com. You'll notice that most top-tier visualizations use direct annotation, not legends.
PS: this post is a short abstract from the 3rd module of Matplotlib Journey.
PPS: I'm working on a tool allowing to create a homepage like mine in less than 30 minutes. Would you like to beta-test it? Drop me an email!
Hope this helps and see you next week!
Yan
Enjoyed this?
Get one short, actionable dataviz tip every Saturday. Join 19,000+ data people.