GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Stem plot (lollipop chart) in matplotlib

Search for a graph

HOME RANKING STEM PLOT MATPLOTLIB

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 a 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 setting 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 mentioning 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

R CODER website
Storytelling with Data

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Data visualization

Data visualization

A practical introduction

Buy on Amazon

See also

Bar chart in plotly

Bar chart in plotly

Tables in matplotlib

Tables in matplotlib

Bar plot in seaborn

Bar plot in seaborn

Radar chart in matplotlib

Radar chart in matplotlib

Bar chart in pandas

Bar chart in pandas

Radar chart in plotly

Radar chart in plotly

Lollipop chart in plotly

Lollipop chart in plotly

Population pyramid in matplotlib

Population pyramid in matplotlib

Bar plot in matplotlib

Bar plot in matplotlib

Wordclouds in Python

Wordclouds in Python

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.