Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make a simple lollipop plot in Matplotlib?
A lollipop plot is a variation of a bar chart where bars are replaced with lines and circles, resembling lollipops. This visualization is effective for showing values across categories while reducing visual clutter compared to traditional bar charts.
Creating a Basic Lollipop Plot
We'll create a lollipop plot using Matplotlib's stem() function with sample data ?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Set figure size
plt.figure(figsize=(10, 6))
# Create sample data
categories = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
values = [23, 45, 56, 78, 32, 67, 89, 12]
# Create the lollipop plot
plt.stem(categories, values, linefmt='b-', markerfmt='ro', basefmt=' ')
plt.title('Simple Lollipop Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.grid(True, alpha=0.3)
plt.show()
Display: A lollipop plot with blue stems and red circular markers
Using Pandas DataFrame
Let's create a more comprehensive example using a DataFrame with sorted values ?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Create DataFrame with random data
np.random.seed(42) # For reproducible results
df = pd.DataFrame({
'group': list(map(chr, range(65, 75))), # A to J
'values': np.random.uniform(10, 100, size=10)
})
# Sort by values for better visualization
ordered_df = df.sort_values(by='values')
# Create lollipop plot
plt.figure(figsize=(12, 6))
x_pos = range(len(ordered_df))
plt.stem(x_pos, ordered_df['values'],
linefmt='gray', markerfmt='o',
markersize=8, markerfacecolor='coral')
# Customize the plot
plt.xticks(x_pos, ordered_df['group'])
plt.title('Lollipop Plot - Sorted by Values', fontsize=16)
plt.xlabel('Groups', fontsize=12)
plt.ylabel('Values', fontsize=12)
plt.grid(True, alpha=0.3, axis='y')
# Add value labels on top of markers
for i, v in enumerate(ordered_df['values']):
plt.text(i, v + 2, f'{v:.1f}', ha='center', va='bottom')
plt.tight_layout()
plt.show()
Display: A sorted lollipop plot with coral markers, gray stems, and value labels
Customizing Lollipop Plots
You can enhance lollipop plots with different colors, styles, and annotations ?
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sales = [85, 92, 78, 96, 88]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# Standard lollipop plot
ax1.stem(categories, sales, linefmt='blue', markerfmt='go', markersize=10)
ax1.set_title('Standard Lollipop Plot')
ax1.set_ylabel('Sales (%)')
ax1.grid(True, alpha=0.3)
# Horizontal lollipop plot
y_pos = np.arange(len(categories))
ax2.stem(sales, y_pos, linefmt='red', markerfmt='bs',
markersize=8, orientation='horizontal')
ax2.set_yticks(y_pos)
ax2.set_yticklabels(categories)
ax2.set_title('Horizontal Lollipop Plot')
ax2.set_xlabel('Sales (%)')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Display: Two subplots showing vertical and horizontal lollipop plots
Key Parameters
| Parameter | Description | Example |
|---|---|---|
linefmt |
Line style and color |
'b-', 'red'
|
markerfmt |
Marker style and color |
'ro', 'gs'
|
basefmt |
Baseline format |
' ' (hidden), 'k-'
|
markersize |
Size of markers |
8, 12
|
Conclusion
Lollipop plots provide a clean alternative to bar charts using plt.stem(). They work well for comparing values across categories while maintaining visual clarity. Use sorting and customization options to enhance readability and visual appeal.
