website/examples/gallery/bar-4.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 [
("Air", [19, 21, 22, 26]),
("Mist", [22, 25, 26, 27]),
("Steam", [24, 30, 34, 40]),
("Wrap", [26, 27, 29, 30]),
]
def make_before(data):
import pandas as pd
import plotly.express as px
from statistics import mean, stdev
frame = pd.DataFrame(
[(method, mean(values), stdev(values)) for method, values in data],
columns=["Method", "Strength", "Spread"],
)
fig = px.bar(
frame,
x="Method",
y="Strength",
color="Method",
error_y="Spread",
color_discrete_sequence=["#0072B2", "#E69F00", "#009E73", "#CC79A7"],
template="simple_white",
)
for (method, values), symbol in zip(
data, ["circle", "circle-open", "diamond", "diamond-open"], strict=True
):
points = [*values, mean(values)]
fig.add_trace(
go.Scatter3d(
x=[method] * len(points),
y=[0] * len(points),
z=points,
mode="markers",
marker=dict(symbol=symbol, size=[v / 2 for v in points], sizemode="diameter"),
name=method,
showlegend=False,
)
)
fig.update_layout(
title_text="Mean strength and specimen results",
xaxis_domain=[0, 0.43],
yaxis_title="Mean (MPa) ± SD",
scene=dict(
camera=dict(eye=dict(x=1.7, y=1.9, z=1.4)),
domain=dict(x=[0.49, 1]),
xaxis_title="Method",
yaxis_visible=False,
zaxis_title="MPa",
),
showlegend=False,
)
return fig
def make_after(data):
from statistics import mean, stdev
fig = go.Figure()
for index, (method, values) in enumerate(data):
fig.add_trace(
go.Scatter(
x=values,
y=[index + v for v in (-0.13, -0.05, 0.05, 0.13)],
mode="markers",
marker=dict(size=9, color="#8C8176"),
name=method,
showlegend=False,
)
)
average = mean(values)
fig.add_trace(
go.Scatter(
x=[average],
y=[index + 0.29],
mode="markers",
marker=dict(size=10, symbol="diamond", color=WARM_EDITORIAL_PALETTE[0]),
error_x=dict(type="data", array=[stdev(values)], visible=True),
name="Mean ± SD",
showlegend=False,
)
)
fig.update_layout(
template="warm_editorial",
title_text="Steam has the highest mean and widest spread",
xaxis=dict(title="Compressive strength (MPa)", range=[16, 43]),
yaxis=dict(
tickmode="array",
tickvals=list(range(len(data))),
ticktext=[method for method, _ in data],
range=[3.7, -0.4],
),
margin=dict(t=110, b=65),
)
fig.add_annotation(
text="Circle = specimen Diamond and whisker = mean ± one standard deviation",
x=0,
y=1.09,
xref="paper",
yref="paper",
xanchor="left",
showarrow=False,
)
return fig
EXAMPLE = {
"kind": "bar",
"source": "Illustrative field trial of four curing methods, with four independently tested specimens per method.",
"before": {
"summary": "Steam has the highest mean strength, but its specimens span 24 to 40 MPa."
},
"after": {
"summary": "Steam has the highest mean strength, but its specimens span 24 to 40 MPa."
},
"principles": {
"channel-discriminability": {
"question": "Which method produced each specimen?",
"book": {
"quote": "which reduce well, maintaining legibility even with individual areas",
"page": 142,
},
"before": {
"caption": "Small open and closed symbols are hard to distinguish in the perspective plot."
},
"after": {
"caption": "Each method has a labeled row and the specimen dots use one shape."
},
},
"dimensions": {
"question": "Does perspective help compare strength?",
"book": {
"quote": "The number of information-carrying (variable) dimensions",
"page": 75,
"line": 2816,
},
"before": {
"caption": "Depth separates no measurements and varying marker diameters exaggerate strength differences."
},
"after": {
"caption": "Horizontal position gives strength on a shared linear scale using constant marker sizes."
},
},
"redundant-ink": {
"question": "What does the color of a mean bar add?",
"book": {"quote": "Erase redundant data-ink", "page": 102, "line": 3877},
"before": {"caption": "The four bar colors repeat the four method labels."},
"after": {
"caption": "The same colors identify specimens and means consistently across methods."
},
},
"distinguish-observed-and-derived": {
"question": "Which marks show measured specimens and which are means?",
"book": {
"quote": "resolve this dilemma by pointing it out in the figure caption",
"page": 133,
},
"before": {
"caption": "The right panel mixes each calculated mean with four specimen points of the same symbol."
},
"after": {
"caption": "Gray circles mark specimens and a teal diamond marks the calculated mean."
},
},
"dynamite-plot": {
"question": "How much do individual strengths vary?",
"book": {
"quote": "Bar plots with error bars hide the underlying data distribution and give unequal visual weight to values above and below the mean",
"page": 193,
},
"before": {
"caption": "The bar heights and whiskers conceal individual values and the adjacent perspective view overlays them."
},
"after": {
"caption": "Four specimen dots per row show the measurements alongside the mean and standard deviation."
},
},
},
}