A horizontal lollipop plot is a simple and effective way to visualize and compare values across categories. In Python, you can create a horizontal lollipop chart using Matplotlib with only a few lines of code.
Why Use a Horizontal Lollipop Plot?
A horizontal lollipop plot is useful when you want to:
- Compare values across multiple categories
- Display rankings clearly
- Handle long category names
- Create a cleaner alternative to a horizontal bar chart
- Highlight differences between observations
The combination of horizontal lines and circular markers makes the chart easy to read while keeping the visualization lightweight.
Steps to Create a Horizontal Lollipop Plot
- Import
matplotlib.pyplot - Prepare category names and numerical values
- Create horizontal lines with
hlines() - Add circular markers with
plot() - Customize the title, labels, and axes
Example: Horizontal Lollipop Plot in Python
import matplotlib.pyplot as plt
categories = ['Python', 'Java', 'C++', 'JavaScript', 'R']
values = [85, 72, 65, 58, 45]
plt.figure(figsize=(8, 5))
plt.hlines(
y=categories,
xmin=0,
xmax=values,
linewidth=2
)
plt.plot(
values,
categories,
'o',
markersize=8
)
plt.xlabel('Popularity Score')
plt.title('Horizontal Lollipop Plot in Python')
plt.show()
Output
The result is a horizontal lollipop chart where each category has a horizontal line extending from zero to its corresponding value. The circular marker at the end of each line makes it easy to compare the categories.
Customizations
You can customize the lollipop plot by:
- Changing
figsizeto control the chart dimensions - Adjusting
linewidthto make the stems thicker or thinner - Changing
markersizefor larger or smaller circles - Sorting the categories to create a ranking
- Adding data labels next to each marker
- Removing unnecessary chart borders for a cleaner design
For example, you can sort the values before plotting:
import matplotlib.pyplot as plt
categories = ['Python', 'Java', 'C++', 'JavaScript', 'R']
values = [85, 72, 65, 58, 45]
categories, values = zip(*sorted(
zip(categories, values),
key=lambda x: x[1]
))
plt.figure(figsize=(8, 5))
plt.hlines(categories, 0, values, linewidth=2)
plt.plot(values, categories, 'o', markersize=8)
plt.xlabel('Popularity Score')
plt.title('Horizontal Lollipop Plot in Python')
plt.show()
Resources
- Matplotlib
hlines() - Matplotlib
plot() - Python data visualization
- Horizontal lollipop chart examples
- Matplotlib chart customization