Custom lollipop plot

logo of a chart:Lollipop

While the previous post explains how to draw a lollipop plot using matplotlib, this post aims to describe the customization you can apply to 3 main parts: the stem, the markers and the baseline. Note that for all of these 3 components, we first build the plot with the stem() function, and then customize it with the setp() function.

Markers

You can easily remove the dots of the stem plot with the parameter markerfmt. It is also possible to change color, shape and size of the markers using setp() function.

# libraries
import matplotlib.pyplot as plt
import numpy as np
 
# create data
values=np.random.uniform(size=40)
 
# plot without markers
plt.stem(values, markerfmt=' ')
plt.show()
 
# change color and shape and size and edges
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(markers, marker='D', markersize=10, markeredgecolor="orange", markeredgewidth=2)
plt.show()

Baseline

The position of the baseline can be changed using bottom argument in the stem() functon. You can also customize the baseline: remove it, change its size, color, and so on using the setp() function.

# libraries
import matplotlib.pyplot as plt
import numpy as np

# create data
values=np.random.uniform(size=100)
 
# position is customized with the `bottom` argument
plt.stem(values, markerfmt=' ', bottom=0.5)
plt.show()
 
# hide the baseline
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(baseline, visible=False)
plt.show()
 
# hide the baseline - second way
plt.stem(values, basefmt=" ")
plt.show()
 
# custom the color and line style of the baseline 
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(baseline, linestyle="-", color="grey", linewidth=6)
plt.show()

Stem

The stems can be customized as well. All the feature that you can apply to a line plot can be applied to the stem: color, width, type, transparency etc.

# libraries
import matplotlib.pyplot as plt
import numpy as np

# create data
values=np.random.uniform(size=100)

# custom the stem lines
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(stemlines, linestyle="-", color="olive", linewidth=0.5 )
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 🙏!