website/examples/gallery/bar-3.py
import plotly.graph_objects as go
import plotly.io as pio
INK = "#1C1917"
MUTED = "#57534E"
WARM_EDITORIAL_PALETTE = [
"#0F766E",
"#C2410C",
"#1D4ED8",
"#A16207",
"#7E22CE",
"#475569",
"#0369A1",
"#1C1917",
]
CANVAS = "#FFF7ED"
RULE = "#E7DDD2"
pio.templates["warm_editorial"] = go.layout.Template(
layout=go.Layout(
colorway=WARM_EDITORIAL_PALETTE,
font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
title={
"x": 0,
"xanchor": "left",
"font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
},
paper_bgcolor=CANVAS,
plot_bgcolor=CANVAS,
xaxis={
"showgrid": False,
"showline": True,
"linecolor": "#A8A29E",
"tickcolor": "#A8A29E",
"tickfont": {"color": MUTED},
"automargin": True,
},
yaxis={
"showgrid": True,
"gridcolor": RULE,
"gridwidth": 1,
"showline": False,
"tickcolor": "#A8A29E",
"tickfont": {"color": MUTED},
"zeroline": False,
"automargin": True,
},
)
)
def load_data():
return [
("Login", "Access", 20, 16),
("Password", "Access", 12, 14),
("Network", "Connection", 15, 13),
("Wi-Fi", "Connection", 9, 8),
("Scanner", "Equipment", 0, 3),
("Printing", "Equipment", None, 4),
]
def make_before(data):
names, _, current, previous = zip(*data, strict=True)
total = sum(v for v in current if v is not None)
shares = [None if v is None else 100 * v / total for v in current]
fig = go.Figure(go.Bar(x=names, y=shares, name="Share of tags"))
for label, counts, dash in [("This month", current, "solid"), ("Last month", previous, "dash")]:
fig.add_trace(
go.Scatter(
x=names,
y=counts,
mode="lines+markers",
name=label,
line=dict(dash=dash),
marker_symbol=["circle", "diamond", "square", "triangle-up", "cross", "x"],
)
)
fig.update_layout(
template="simple_white",
title_text="Share of service desk requests",
yaxis_title="Percent",
showlegend=True,
)
return fig
def make_after(data):
from plotly.subplots import make_subplots
fig = make_subplots(
rows=1, cols=2, shared_yaxes=True, subplot_titles=("Last month", "This month")
)
rows = [f"{group} · {tag}" for tag, group, _, _ in data]
for col, field in [(1, 3), (2, 2)]:
for index, (_, _, current, previous) in enumerate(data):
value = previous if field == 3 else current
if value is None:
fig.add_annotation(
x=0,
y=rows[index],
text="Not recorded",
showarrow=False,
xanchor="left",
row=1,
col=col,
)
else:
fig.add_trace(
go.Bar(
x=[value],
y=[rows[index]],
orientation="h",
marker_color=WARM_EDITORIAL_PALETTE[0],
text=[f"{value}%"],
textposition="outside",
cliponaxis=False,
),
row=1,
col=col,
)
fig.update_yaxes(categoryorder="array", categoryarray=rows, autorange="reversed")
fig.update_xaxes(title_text="Share of 100 tickets (%)", range=[0, 26], dtick=10)
fig.update_layout(
template="warm_editorial",
title_text="A ticket can carry several issue tags",
showlegend=False,
margin=dict(t=88),
)
return fig
EXAMPLE = {
"kind": "bar",
"source": "Illustrative service desk survey. Each of 100 tickets could carry several tags. Counts are for this month and the preceding month.",
"before": {
"summary": "Twenty tickets carried a login tag this month. The printing count was unavailable."
},
"after": {
"summary": "Twenty tickets carried a login tag this month. The printing count was unavailable."
},
"principles": {
"channel-data-type": {
"question": "Which tags are most frequent?",
"book": {
"quote": "Superposed data sets must be readily visually assembled.",
"page": 51,
},
"before": {
"caption": "Six marker shapes compete with the counts and have no order of magnitude."
},
"after": {
"caption": "Aligned lengths on one percentage scale encode the ordered quantities."
},
},
"consolidate-categories-by-meaning": {
"question": "Which tags concern access or connectivity?",
"book": {
"quote": "depending upon the degree of race detail shown separately",
"page": 180,
},
"before": {
"caption": "An undifferentiated list leaves readers to infer how the tags belong together."
},
"after": {
"caption": "Adjacent rows repeat the issue family so related tags can be read together."
},
},
"puzzle": {
"question": "Which curve shows the current reporting month?",
"book": {"quote": "The danger of multifunctioning elements", "page": 140, "line": 5260},
"before": {
"caption": "Readers must decode the two month names in a separate line key."
},
"after": {"caption": "The month appears above each column of tag counts."},
},
"additive-part-to-whole": {
"question": "What share of tickets had login trouble?",
"book": {"quote": "to show amounts, we can group or stack", "page": 38},
"before": {
"caption": "Bars divide each tag count by all recorded tags even though a ticket may have several tags."
},
"after": {
"caption": "Both panels use the number of tickets as their denominator and state the multiple tag rule."
},
},
"zero-and-missing": {
"question": "Was printing trouble absent or unrecorded?",
"book": {"quote": "the plain dots register a zero reading", "page": 146, "line": 5483},
"before": {
"caption": "The scanner and printing bars both disappear despite different reporting states."
},
"after": {
"caption": "Scanner has a zero percent label and the printing row says Not recorded."
},
},
},
}