Graphify-Labs/graphify · error · ImportError

matplotlib not installed. Run: pip install matplotlib

Error message

matplotlib not installed. Run: pip install matplotlib

What it means

Raised by graphify's Markdown graph exporter when matplotlib is not importable. matplotlib is an optional dependency used only for the static PNG/SVG community layout (spring_layout on a dark figure), so graphify guards the import and converts the underlying ImportError into an actionable message rather than failing at module load.

Source

Thrown at graphify/export.py:1102

    communities: dict[int, list[str]],
    output_path: str,
    community_labels: dict[int, str] | None = None,
    figsize: tuple[int, int] = (20, 14),
) -> None:
    """Export graph as an SVG file using matplotlib + spring layout.

    Lightweight and embeddable - works in Obsidian notes, Notion, GitHub READMEs,
    and any markdown renderer. No JavaScript required.

    Node size scales with degree. Community colors match the HTML output.
    """
    try:
        import matplotlib
        matplotlib.use("Agg")
        import matplotlib.pyplot as plt
        import matplotlib.patches as mpatches
    except ImportError as e:
        raise ImportError("matplotlib not installed. Run: pip install matplotlib") from e

    node_community = _node_community_map(communities)

    fig, ax = plt.subplots(figsize=figsize, facecolor="#1a1a2e")
    ax.set_facecolor("#1a1a2e")
    ax.axis("off")

    pos = nx.spring_layout(G, seed=42, k=2.0 / (G.number_of_nodes() ** 0.5 + 1))

    degree = dict(G.degree())
    max_deg = max(degree.values(), default=1) or 1

    node_colors = [COMMUNITY_COLORS[node_community.get(n, 0) % len(COMMUNITY_COLORS)] for n in G.nodes()]
    node_sizes = [300 + 1200 * (degree.get(n, 1) / max_deg) for n in G.nodes()]

    # Draw edges - dashed for non-EXTRACTED
    for u, v, data in G.edges(data=True):
        conf = data.get("confidence", "EXTRACTED")

View on GitHub (pinned to 7fe58b0b0f)

Solutions

  1. pip install matplotlib
  2. Or reinstall graphify with the visualization extra if one is documented (e.g. `pip install graphify[viz]`)
  3. If the install reports success but the error persists, verify in the same interpreter: `python -c "import matplotlib"` and check for missing system libraries
  4. If you don't need the static image, use the HTML or DOT exporters instead, which have no matplotlib dependency

Example fix

# before
graphify export --markdown graph.png   # ImportError: matplotlib not installed

# after
pip install matplotlib
graphify export --markdown graph.png
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util

matplotlib_ok = importlib.util.find_spec("matplotlib") is not None
if not matplotlib_ok:
    print("matplotlib missing - skipping markdown image export")
else:
    export_markdown_graph(G, communities, "graph.png")

Try / catch

try:
    export_markdown_graph(G, communities, "graph.png")
except ImportError as e:
    if "matplotlib" in str(e):
        print(f"skipping image export: {e}")
    else:
        raise

Prevention

When it happens

Trigger: Calling the markdown-embedding export function (to_markdown_graph / export with matplotlib output) in an environment where `import matplotlib` (or matplotlib.pyplot) raises ImportError — e.g. the base graphify install without the viz extra, or a venv where matplotlib was never installed.

Common situations: Installing graphify via `pip install graphify` (core extras only) and then requesting the matplotlib-based markdown graph; running in a slim container/CI image; a broken matplotlib install (missing system libs like libpng) also surfaces as ImportError here.

Related errors


AI-assisted analysis of Graphify-Labs/graphify@7fe58b0b0f (2026-08-14). Data as JSON: /api/errors/a9b0e50728e96f90. Report an issue: GitHub.