0
0
Matplotlibdata~5 mins

Lollipop charts in Matplotlib

Choose your learning style9 modes available
Introduction

Lollipop charts help you compare values clearly using dots connected to a baseline by lines. They are easy to read and look neat.

When you want to compare different categories with their values visually.
When you want a simple chart that is less cluttered than bar charts.
When you want to highlight exact values with dots on a line.
When you want to show ranking or order of items clearly.
When you want a fresh alternative to bar charts for presentations.
Syntax
Matplotlib
import matplotlib.pyplot as plt

plt.stem(x, y, basefmt=" ", use_line_collection=True)
plt.show()

plt.stem() creates the lollipop chart with lines and dots.

basefmt=" " removes the baseline line for cleaner look.

Examples
Basic lollipop chart with default baseline.
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [4, 5, 6]
plt.stem(x, y)
plt.show()
Lollipop chart with categorical x-axis and no baseline line.
Matplotlib
import matplotlib.pyplot as plt

x = ['A', 'B', 'C']
y = [10, 15, 7]
plt.stem(x, y, basefmt=" ")
plt.show()
Customize colors of dots and lines in the lollipop chart.
Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [5, 3, 8, 6]
markerline, stemlines, baseline = plt.stem(x, y)
markerline.set_markerfacecolor('red')
stemlines.set_color('green')
plt.show()
Sample Program

This program shows sales numbers for different fruits using a lollipop chart. Each fruit has a dot connected to the baseline by a line, making it easy to compare sales.

Matplotlib
import matplotlib.pyplot as plt

# Data for fruits and their sales
fruits = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']
sales = [50, 30, 40, 20, 60]

# Create lollipop chart
plt.stem(fruits, sales, basefmt=" ", use_line_collection=True)

# Add title and labels
plt.title('Fruit Sales Lollipop Chart')
plt.xlabel('Fruit')
plt.ylabel('Sales')

plt.show()
OutputSuccess
Important Notes

Lollipop charts are great for small to medium datasets.

Use use_line_collection=True for better performance with many points.

You can customize colors and markers for better visuals.

Summary

Lollipop charts use lines and dots to show values clearly.

They are simpler and cleaner than bar charts.

Use plt.stem() in matplotlib to create them easily.