Lollipop chart in plotly

Lollipop chart in plotly

A lollipop chart is an alternative to a bar chart that replaces each bar with a thin stem and a marker at the end, which reduces visual clutter when comparing many categories. Plotly express does not provide a shortcut for it, so it is built by combining a line shape per category (the stem) with a Scatter trace in marker mode (the candy).

import plotly.graph_objects as go

# Sample data
labels = ["G1", "G2", "G3", "G4", "G5"]
value = [12, 22, 16, 38, 12]

fig = go.Figure()
for label, v in zip(labels, value):
    fig.add_shape(type = "line", x0 = 0, y0 = label, x1 = v, y1 = label,
                 line = dict(color = "gray", width = 2))

fig.add_trace(go.Scatter(
    x = value, y = labels, mode = "markers",
    marker = dict(size = 14, color = "#4C72B0")))

fig.show()

Colors

Change the stem color inside the loop and the marker color through the marker dict of the Scatter trace.

import plotly.graph_objects as go

# Sample data
labels = ["G1", "G2", "G3", "G4", "G5"]
value = [12, 22, 16, 38, 12]

fig = go.Figure()
for label, v in zip(labels, value):
    fig.add_shape(type = "line", x0 = 0, y0 = label, x1 = v, y1 = label,
                 line = dict(color = "#B0B0B0", width = 2))

fig.add_trace(go.Scatter(
    x = value, y = labels, mode = "markers",
    marker = dict(size = 14, color = "#C44E52")))

fig.show()

Marker size

Adjust marker.size to make the candy bigger or smaller.

import plotly.graph_objects as go

# Sample data
labels = ["G1", "G2", "G3", "G4", "G5"]
value = [12, 22, 16, 38, 12]

fig = go.Figure()
for label, v in zip(labels, value):
    fig.add_shape(type = "line", x0 = 0, y0 = label, x1 = v, y1 = label,
                 line = dict(color = "gray", width = 2))

fig.add_trace(go.Scatter(
    x = value, y = labels, mode = "markers",
    marker = dict(size = 24, color = "#4C72B0")))

fig.show()

Sorted lollipop chart

Sort the categories by value before building the shapes and the scatter trace, so the chart reads as a ranking.

import plotly.graph_objects as go

# Sample data
labels = ["G1", "G2", "G3", "G4", "G5"]
value = [12, 22, 16, 38, 12]

order = sorted(zip(value, labels))
value, labels = zip(*order)

fig = go.Figure()
for label, v in zip(labels, value):
    fig.add_shape(type = "line", x0 = 0, y0 = label, x1 = v, y1 = label,
                 line = dict(color = "gray", width = 2))

fig.add_trace(go.Scatter(
    x = value, y = labels, mode = "markers",
    marker = dict(size = 14, color = "#4C72B0")))

fig.show()

Vertical orientation

Swap the X and Y roles in both the shapes and the scatter trace to draw the stems vertically instead.

import plotly.graph_objects as go

# Sample data
labels = ["G1", "G2", "G3", "G4", "G5"]
value = [12, 22, 16, 38, 12]

fig = go.Figure()
for label, v in zip(labels, value):
    fig.add_shape(type = "line", x0 = label, y0 = 0, x1 = label, y1 = v,
                 line = dict(color = "gray", width = 2))

fig.add_trace(go.Scatter(
    x = labels, y = value, mode = "markers",
    marker = dict(size = 14, color = "#4C72B0")))

fig.show()
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also