Heat Map

Author

Eric W. Weisstein
August 9, 2026

This notebook downloaded from https://mathworld.wolfram.com/notebooks/Graphics/HeatMap.nb.

For more information, see Eric's MathWorld entry https://mathworld.wolfram.com/HeatMap.html.

©2026 Wolfram Research, Inc. except for portions noted otherwise

One-time Python setup

This notebook uses the MIT-licensed matplotsoccer Python package to produce a soccer-player position heat map from deterministic synthetic coordinates. No match or player data are used.

Evaluate the following cell once. It creates a dedicated Python virtual environment under $UserBaseDirectory and installs matplotsoccer and its plotting dependencies there.

Module[{basePython,environment,installResult,python,setupResult},environment=FileNameJoin[{$UserBaseDirectory,"ApplicationData","MathWorld","Python","matplotsoccer"}];python=FileNameJoin[{environment,"bin","python3"}];setupResult=If[FileExistsQ[python],Association["ExitCode"->0,"StandardError"->""],basePython=SelectFirst[(FileNameJoin[{#1,"python3"}]&)/@StringSplit[Environment["PATH"],":"],FileExistsQ,Missing["NotFound"]];If[MissingQ[basePython],Failure["PythonNotFound",Association["MessageTemplate"->"No python3 executable was found on PATH."]],RunProcess[{basePython,"-m","venv",environment}]]];If[FailureQ[setupResult],setupResult,If[setupResult["ExitCode"]===0&&FileExistsQ[python],installResult=RunProcess[{python,"-m","pip","install","--disable-pip-version-check","--no-input","matplotsoccer","matplotlib","pandas","scipy"}];If[installResult["ExitCode"]===0,installResult,Failure["PythonPackageInstallationError",Association["MessageTemplate"->"The Python packages could not be installed. Details: `1`","MessageParameters"->{StringTrim[installResult["StandardError"]]}]]],Failure["PythonEnvironmentError",Association["MessageTemplate"->"The Python virtual environment could not be created. Details: `1`","MessageParameters"->{StringTrim[setupResult["StandardError"]]}]]]]]

Generate the heat map

Evaluate the following cell to run matplotsoccer in a Python subprocess, save vector SVG, and import the SVG as a Wolfram Language Graphics expression.

Module[{environment,graphic,output,python,pythonCode,result},environment=FileNameJoin[{$UserBaseDirectory,"ApplicationData","MathWorld","Python","matplotsoccer"}];python=FileNameJoin[{environment,"bin","python3"}];pythonCode=StringRiffle[{"import sys","import matplotlib","matplotlib.use('Agg')","import matplotlib.pyplot as plt","import matplotsoccer","import numpy as np","import pandas as pd","from scipy.ndimage import gaussian_filter","","output_path = sys.argv[1]","rng = np.random.default_rng(20260809)","positions = np.vstack([","    rng.multivariate_normal([55, 34], [[150, 0], [0, 70]], 900),","    rng.multivariate_normal([78, 22], [[55, 0], [0, 32]], 350),","    rng.multivariate_normal([80, 49], [[45, 0], [0, 28]], 300)","])","x = pd.Series(np.clip(positions[:, 0], 0, 105))","y = pd.Series(np.clip(positions[:, 1], 0, 68))","counts = matplotsoccer.count(x, y, n=35, m=23)","smoothed = gaussian_filter(counts, sigma=1.25)","ax = matplotsoccer.field(color='green', show=False)","x_edges = np.linspace(0, 105, smoothed.shape[1] + 1)","y_edges = np.linspace(0, 68, smoothed.shape[0] + 1)","ax.pcolormesh(","    x_edges, y_edges, smoothed[::-1, :], cmap='hot',","    shading='flat', edgecolors='face', linewidth=0.2,","    antialiased=False, snap=True, zorder=7000",")","plt.savefig(","    output_path, format='svg', bbox_inches='tight', pad_inches=0.02,","    facecolor='white'",")","plt.close('all')"},"\n"];If[FileExistsQ[python],output=FileNameJoin[{$TemporaryDirectory,"HeatMap-"<>CreateUUID[]<>".svg"}];result=RunProcess[{python,"-c",pythonCode,output}];If[result["ExitCode"]===0&&FileExistsQ[output],graphic=Import[output,"Graphics"];DeleteFile[output];graphic,If[FileExistsQ[output],DeleteFile[output]];Failure["PythonError",Association["MessageTemplate"->"Python could not generate the heat map. Details: `1`","MessageParameters"->{StringTrim[result["StandardError"]]}]]],Failure["PythonEnvironmentNotFound",Association["MessageTemplate"->"Evaluate the one-time Python setup cell first."]]]]

Package source: TomDecroos/matplotsoccer (MIT License).