#181 Custom lollipop plot

The graph #180 explains how to make a lollipop plot with Matplotlib, whatever the shape of your data. This page aims to describe the customization you can apply to the 3 main parts: the stem, the markers and the baseline. Note that for all of these 3 components, we first build the stem plot with the stem() function, and then customize it with the plt.setp function.

 

Markers


You can easily remove the dots of the stem plot (left). Or custom its size, color and edges:


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

# create data
import numpy as np
values=np.random.uniform(size=40)

# plot with no marker
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


It can be really interesting to move the position of the baseline: the graph on the has a really different look and can pass a different message. You can also custom the baseline: remove it, change its size, color, and so on.


# 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 it
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(baseline, visible=False)
plt.show()

# note that this works as well
plt.stem(values, basefmt=" ")

# custom it
(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 stem: color, width, type, transparency etc..


# custom it
(markers, stemlines, baseline) = plt.stem(values)
plt.setp(stemlines, linestyle="-", color="olive", linewidth=0.5 )

Leave a Reply

Your email address will not be published.