Lollipop plot with conditional color

logo of a chart:Lollipop

This post explains how to change the color of lines depending on the y-axis values in a lollipop plot using the vlines() and the scatter() functions of matplotlib library.

In the following example, while the lines which have negative values on the y-axis are blue, the others are orange. This conditional color feature makes the chart clearer for the readers. You can easily create a color variable depending on the y-axis values. Then, you just need to pass this color variable to the functions vlines() and scatter() as a color argument.

# libraries
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
 
# Data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x) + np.random.uniform(size=len(x)) - 0.2
 
# Create a color if the y axis value is equal or greater than 0
my_color = np.where(y>=0, 'orange', 'skyblue')
 
# The vertical plot is made using the vline function
plt.vlines(x=x, ymin=0, ymax=y, color=my_color, alpha=0.4)
plt.scatter(x, y, color=my_color, s=1, alpha=1)
 
# Add title and axis names
plt.title("Evolution of the value of ...", 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 🙏!