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 — Lollipop chart in plotly

Search for a graph

HOME RANKING LOLLIPOP CHART PLOTLY

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()
R CODER website
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Storytelling with Data

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also

Circular barplot in matplotlib

Circular barplot in matplotlib

Radar chart in matplotlib

Radar chart in matplotlib

Stem plot (lollipop chart) in matplotlib

Stem plot (lollipop chart) in matplotlib

Bar plot in seaborn

Bar plot in seaborn

Bar chart in pandas

Bar chart in pandas

Bar chart in plotly

Bar chart in plotly

Population pyramid in matplotlib

Population pyramid in matplotlib

Radar chart in plotly

Radar chart in plotly

Bar plot in matplotlib

Bar plot in matplotlib

Tables in matplotlib

Tables in matplotlib

You are visitor 01402792 — 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.