Highlight a group in a lollipop plot

logo of a chart:Lollipop

While the previous post showing how to draw a horizontal lollipop plot, this post describes how to highlight a specific group in a lollipop plot using different color or making the marker size bigger.

The previous post explains how to create a horizontal lollipop plot using the hlines() function of matplotlib. In the example below, "group B" is shown with different color and marker size. In order to do it, my_color variable is defined; 'orange' for group B and 'skyblue' for the remaining groups. This color variable is passed to the hlines() function as an argument. For changing the marker sizes, the scatter() function of matplotlib is used.

# libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
# Create a dataframe
df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'values':np.random.uniform(size=20) })
 
# Reorder it based on values:
ordered_df = df.sort_values(by='values')
my_range=range(1,len(df.index)+1)
 
# Create a color if the group is "B"
my_color=np.where(ordered_df ['group']=='B', 'orange', 'skyblue')
my_size=np.where(ordered_df ['group']=='B', 70, 30)
 
# The horizontal plot is made using the hline() function
plt.hlines(y=my_range, xmin=0, xmax=ordered_df['values'], color=my_color, alpha=0.4)
plt.scatter(ordered_df['values'], my_range, color=my_color, s=my_size, alpha=1)
 
# Add title and axis names
plt.yticks(my_range, ordered_df['group'])
plt.title("What about the B group?", loc='left')
plt.xlabel('Value of the variable')
plt.ylabel('Group')

# show the graph
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!