Stem plot (lollipop chart) in matplotlib

The stem function

The matplotlib stem function allows creating stem plots, also known as lollipop charts, in Python. You will need to pass your input data to the function with the locations and values to create a simple stem plot in Python.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y)
# plt.show()

Basic stem plot in Python

Horizontal stem plot

By default, the stem plots are displayed in vertical mode, but you can create an horizontal chart setting the orientation argument to "horizontal", as in the example below.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, orientation = "horizontal")
# plt.show()

Horizontal stem plot in matplotlib

Bottom

Note that the base line in red is set to zero by default, but you can override its position with the bottom argument.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, bottom = 60)
# plt.show()

Change the base line of a lollipop chart in Python

Styling of the lollipop charts

The stem function provides three arguments to style the plot: linefmt for the lines, markerfmt for the markers and basefmt for the base line.

Color of the lines

In order to change the color of the lines you can pass a new color to the linefmt argument, as in the example below.

Customize the color of the lines of a stem plot in Python

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, linefmt = 'black')
# plt.show()

Line style

Note that the same argument used to change the line color can also be used to customize the line style.

Change the line style of a matplotlib lollipop chart

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, linefmt = '--')
# plt.show()

Color and style of the lines at the same time

Moreover, this argument also allows to set a new color and line style at the same time, indicating a new color (e.g. k for black) and line type (-- for a dashed line).

Set the style and color of the lines of a stem graph in Python

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, linefmt = 'k--')
# plt.show()

Markers styling

The markerfmt argument allows customizing the color and shape of the markers of the lollipop. In the example below we are setting red (r) diamonds (D) as marker.

Custom marker shape and style of a Python lollipop chart

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, markerfmt = 'Dr')
# plt.show()

Base line customization

The base line color and style can be customized the same way as the other lines but using the basefmt argument. In the following example we are setting a dotted black line.

Customization of the base line of the Python stem plot

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, basefmt = 'k:')
# plt.show()

Stem plot by group

Finally, it is worth to mention that you can create a lollipop chart by group in Python just writing a new line. In order to add a legend set the label with the label argument and use the legend function.

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.arange(2, 12, 1)
y = x ** 2

x2 = np.arange(2, 12, 1)
y2 = -y

# Stem plot
fig, ax = plt.subplots()
ax.stem(x, y, 'b', markerfmt = 'bo', label = "Group 1")
ax.stem(x2, y2, 'g', markerfmt = 'go', label = "Group 2")
ax.legend()
# plt.show()

Lollipop chart with several groups in Python

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon

See also