Cplot
:rainbow: Plot complex functions
Install / Use
/learn @nschloe/CplotREADME
cplot helps plotting complex-valued functions in a visually appealing manner.
Install with
pip install cplot
and use as
import numpy as np
import cplot
def f(z):
return np.sin(z**3) / z
plt = cplot.plot(
f,
(-2.0, +2.0, 400),
(-2.0, +2.0, 400),
# abs_scaling=lambda x: x / (x + 1), # how to scale the lightness in domain coloring
# contours_abs=2.0,
# contours_arg=(-np.pi / 2, 0, np.pi / 2, np.pi),
# emphasize_abs_contour_1: bool = True,
# add_colorbars: bool = True,
# add_axes_labels: bool = True,
# saturation_adjustment: float = 1.28,
# min_contour_length = None,
# linewidth = None,
)
plt.show()
Historically, plotting of complex functions was in one of three ways
| <img src="https://nschloe.github.io/cplot/sinz3z-abs.svg" width="70%"> | <img src="https://nschloe.github.io/cplot/sinz3z-arg.svg" width="70%"> | <img src="https://nschloe.github.io/cplot/sinz3z-contours.svg" width="70%"> | | :--------------------------------------------------------------------: | :--------------------------------------------------------------------: | :-------------------------------------------------------------------------: | | Only show the absolute value; sometimes as a 3D plot | Only show the phase/the argument in a color wheel (phase portrait) | Show contour lines for both arg and abs |
Combining all three of them gives you a cplot:
<p align="center"> <img src="https://nschloe.github.io/cplot/sinz3z.svg" width="60%"> </p>See also Wikipedia: Domain coloring.
Features of this software:
- cplot uses OKLAB, a perceptually uniform color space for the argument colors. This avoids streaks of colors occurring with other color spaces, e.g., HSL.
- The contour
abs(z) == 1is emphasized, other abs contours are at 2, 4, 8, etc. and 1/2, 1/4, 1/8, etc., respectively. This makes it easy to tell the absolte value precisely. - For
arg(z) == 0, the color is green, forarg(z) == pi/2it's blue, forarg(z) = -pi / 2it's orange, and forarg(z) = piit's pink.
Other useful functions:
<!--pytest.mark.skip--># There is a tripcolor function as well for triangulated 2D domains
cplot.tripcolor(triang, z)
# The function get_srgb1 returns the SRGB1 triple for every complex input value.
# (Accepts arrays, too.)
z = 2 + 5j
val = cplot.get_srgb1(z)
Riemann sphere
<p align="center"> <img src="https://nschloe.github.io/cplot/riemann-log.png" width="60%"> </p>cplot can also plot functions on the Riemann sphere, a mapping of the complex plane to the unit ball.
<!--pytest-codeblocks:importorskip(pyvista)-->import cplot
import numpy as np
cplot.riemann_sphere(np.log)
Gallery
All plots are created with default settings.
| <img src="https://nschloe.github.io/cplot/z1.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z2.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z3.png" width="70%"> |
| :------------------------------------------------------------: | :------------------------------------------------------------: | :------------------------------------------------------------: |
| z ** 1 | z ** 2 | z ** 3 |
| <img src="https://nschloe.github.io/cplot/1z.png" width="70%"> | <img src="https://nschloe.github.io/cplot/1z2.png" width="70%"> | <img src="https://nschloe.github.io/cplot/1z3.png" width="70%"> |
| :------------------------------------------------------------: | :-------------------------------------------------------------: | :-------------------------------------------------------------: |
| 1 / z | 1 / z ** 2 | 1 / z ** 3 |
| <img src="https://nschloe.github.io/cplot/moebius1.png" width="70%"> | <img src="https://nschloe.github.io/cplot/moebius2.png" width="70%"> | <img src="https://nschloe.github.io/cplot/moebius3.png" width="70%"> |
| :------------------------------------------------------------------: | :---------------------------------------------------------------------------------------: | :------------------------------------------------------------------: |
| (z + 1) / (z - 1) | Another Möbius transformation | A third Möbius transformation |
| <img src="https://nschloe.github.io/cplot/re.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z-absz.png" width="70%"> | <img src="https://nschloe.github.io/cplot/conj.png" width="70%"> |
| :------------------------------------------------------------: | :----------------------------------------------------------------: | :--------------------------------------------------------------: |
| np.real | z / abs(z) | np.conj |
| <img src="https://nschloe.github.io/cplot/z6+1.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z6-1.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z-6+1.png" width="70%"> |
| :--------------------------------------------------------------: | :--------------------------------------------------------------: | :---------------------------------------------------------------: |
| z ** 6 + 1 | z ** 6 - 1 | z ** (-6) + 1 |
| <img src="https://nschloe.github.io/cplot/zz.png" width="70%"> | <img src="https://nschloe.github.io/cplot/1zz.png" width="70%"> | <img src="https://nschloe.github.io/cplot/z1z.png" width="70%"> |
| :------------------------------------------------------------: | :-------------------------------------------------------------: | :-------------------------------------------------------------: |
| z ** z | (1/z) ** z | z ** (1/z) |
| <img src="https://nschloe.github.io/cplot/root2.png" width="70%"> | <img src="https://nschloe.github.io/cplot/root3.png" width="70%"> | <img src="https://nschloe.github.io/cplot/root4.png" width="70%"> |
| :---------------------------------------------------------------: | :---------------------------------------------------------------: | :---------------------------------------------------------------: |
| np.sqrt | z**(1/3) | z**(1/4) |
| <img src="https://nschloe.github.io/cplot/log.png" width="70%"> | <img src="https://nschloe.github.io/cplot/exp.png" width="70%"> | <img src="https://nschloe.github.io/cplot/exp2.png" width="70%"> |
| :-------------------------------------------------------------: | :-------------------------------------------------------------: | :--------------------------------------------------------------: |
| np.log | np.exp | np.exp2 |
| <img src="https://nschloe.github.io/cplot/exp1z.png" width="70%"> | <img src="https://nschloe.github.io/cplot/zsin1z.png" width="70%"> | <img src="https://nschloe.github.io/cplot/cos1z.png" width="70%"> |
| :---------------------------------------------------------------: | :----------------------------------------------------------------: | :---------------------------------------------------------------: |
| np.exp(1 / z) | z * np.sin(1 / z) | np.cos(1 / z)
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
claude-opus-4-5-migration
92.1kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
model-usage
343.3kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
