SkillAgentSearch skills...

Cplot

:rainbow: Plot complex functions

Install / Use

/learn @nschloe/Cplot
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center"> <a href="https://github.com/nschloe/cplot"><img alt="cplot" src="https://nschloe.github.io/cplot/cplot-logo.svg" width="50%"></a> <p align="center">Plot complex-valued functions with style.</p> </p>

PyPi Version PyPI pyversions DOI GitHub stars Downloads

Discord

gh-actions codecov Code style: black

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) == 1 is 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, for arg(z) == pi/2 it's blue, for arg(z) = -pi / 2 it's orange, and for arg(z) = pi it'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 |

<details> <summary>Many more plots</summary>

| <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

View on GitHub
GitHub Stars168
CategoryDevelopment
Updated27d ago
Forks22

Languages

Python

Security Score

100/100

Audited on Mar 4, 2026

No findings