svgvariogram
Semivariogram
How far does resemblance reach before sites are no more alike than any two sites?
How to read it
- Read x as the distance between two sites and y as how much they differ on average.
- Follow the rise: near sites are more alike than far ones until the curve levels off at the sill.
- Read the distance where it levels off as the range, and the height at distance zero as the nugget.
- Callout 1: The nugget: difference between sites at almost no distance.
- Callout 2: The sill: pairs this far apart are no more alike than any two sites.
- Callout 3: Band from shuffled values: points inside it show no spatial structure.
- Callout 4: Larger points are classes with more pairs.
Watch out
- A curve that keeps rising usually means a trend across the map, not a long range. Remove the trend and look at the residuals.
- Classes with few pairs are noisy. Don't read much into small points.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Semivariance rises with distance when near sites are more alike than far ones. It levels off at the sill, near the variance, at about the practical range.
- The jump at distance 0 is the nugget: measurement error or variation at scales below the closest sites.
- Points inside the shuffled band are no more alike than random pairs. Classes with few pairs are noisy, so point area shows the pairs.
Use with care
A semivariance that keeps rising usually means a trend across the map, not a long range: remove the trend and look at its residuals.
# Semivariogram — Codesplash Chart Lab (recipe "variogram")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("variogram-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
H = "distance"
GAMMA = "semivariance"
PAIRS = "pairs"
LO = "shuffled low"
HI = "shuffled high"
FITTED = "fitted"
VARIANCE = None # the data's variance, when the analysis reports it
d = df.dropna(subset=[H, GAMMA]).sort_values(H)
h = d[H].to_numpy(dtype=float)
area = d[PAIRS].to_numpy(dtype=float) / d[PAIRS].max() if PAIRS else np.full(len(d), 0.5)
fig, ax = plt.subplots(figsize=(8, 4.5))
if LO and HI:
ax.fill_between(h, d[LO], d[HI], color=PALETTE[0], alpha=0.14, linewidth=0, label="shuffled values")
if VARIANCE is not None:
ax.axhline(VARIANCE, color=REF, linestyle="--", linewidth=1, label="variance")
if FITTED:
ax.plot(h, d[FITTED], color=PALETTE[1], linewidth=1.8, label="fitted model")
ax.scatter(h, d[GAMMA], s=20 + 120 * area, color=PALETTE[0], alpha=0.75, linewidths=0.7, edgecolors="white", label="semivariance (area = pairs)")
ax.set(xlim=(0, h.max() * 1.05), ylim=(0, None), xlabel=H, ylabel=GAMMA)
ax.legend(frameon=False, fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads variogram-demo.csv · app theme from the optional codesplash helper
# Semivariogram — Codesplash Chart Lab (recipe "variogram")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("variogram-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
H = "distance"
GAMMA = "semivariance"
PAIRS = "pairs"
LO = "shuffled low"
HI = "shuffled high"
FITTED = "fitted"
VARIANCE = None # the data's variance, when the analysis reports it
d = df.dropna(subset=[H, GAMMA]).sort_values(H)
h = d[H].to_numpy(dtype=float)
area = d[PAIRS].to_numpy(dtype=float) / d[PAIRS].max() if PAIRS else np.full(len(d), 0.5)
fig = go.Figure()
if LO and HI:
fig.add_trace(go.Scatter(x=np.r_[h, h[::-1]], y=np.r_[d[HI], d[LO][::-1]], fill="toself", mode="none", fillcolor="rgba(120,150,200,0.22)", name="shuffled values"))
if VARIANCE is not None:
fig.add_hline(y=VARIANCE, line_dash="dash", line_color=REF)
if FITTED:
fig.add_trace(go.Scatter(x=h, y=d[FITTED], mode="lines", name="fitted model", line=dict(color=PALETTE[1], width=2)))
fig.add_trace(go.Scatter(x=h, y=d[GAMMA], mode="markers", name="semivariance (area = pairs)", marker=dict(size=6 + 10 * np.sqrt(area), color=PALETTE[0], opacity=0.75)))
fig.update_layout(xaxis_title=H, yaxis_title=GAMMA, xaxis_rangemode="tozero", yaxis_rangemode="tozero")
fig.show()Needs pandas, numpy, plotly · reads variogram-demo.csv · app theme from the optional codesplash helper
# Semivariogram — Codesplash Chart Lab (recipe "variogram")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("variogram-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
h_col <- "distance"
gamma_col <- "semivariance"
pairs_col <- "pairs"
lo_col <- "shuffled low"
hi_col <- "shuffled high"
fitted_col <- "fitted"
variance_line <- NULL # the data's variance, when the analysis reports it
d <- df[!is.na(df[[h_col]]) & !is.na(df[[gamma_col]]), , drop = FALSE]
d <- d[order(d[[h_col]]), , drop = FALSE]
d$pair_count <- if (!is.null(pairs_col)) d[[pairs_col]] else 1
p <- ggplot(d, aes(.data[[h_col]], .data[[gamma_col]]))
if (!is.null(lo_col) && !is.null(hi_col)) p <- p + geom_ribbon(aes(ymin = .data[[lo_col]], ymax = .data[[hi_col]]), fill = cs_cat[1], alpha = 0.14)
if (!is.null(variance_line)) p <- p + geom_hline(yintercept = variance_line, linetype = "dashed", colour = ref_col)
if (!is.null(fitted_col)) p <- p + geom_line(aes(y = .data[[fitted_col]]), colour = cs_cat[2], linewidth = 0.9)
p <- p + geom_point(aes(size = pair_count), colour = cs_cat[1], alpha = 0.75) + scale_size_area(max_size = 4.5, name = "pairs") + expand_limits(x = 0, y = 0) + labs(x = h_col, y = gamma_col) + theme_cs
print(p)Needs ggplot2 · reads variogram-demo.csv · app theme from the optional codesplashr helper