Skip to main content

Label lines in matplotlib.

Project description

matplotlib-label-lines

Build status Supported Python Versions PyPI codecov

Easily label line(s) using matplotlib.

The code is heavily based on http://stackoverflow.com/questions/16992038/inline-labels-in-matplotlib (original code from NauticalMile).

Install

Just do:

pip install matplotlib-label-lines

You can try it online on binder Binder, get some inspiration from the example or from the following script:

import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import chi2, loglaplace

from labellines import labelLine, labelLines

X = np.linspace(0, 1, 500)
A = [1, 2, 5, 10, 20]
funcs = [np.arctan, np.sin, loglaplace(4).pdf, chi2(5).pdf]

fig, axes = plt.subplots(ncols=2, nrows=3, constrained_layout=True, figsize=(8, 8))

axes = axes.flatten()

ax = axes[0]
for a in A:
    ax.plot(X, np.arctan(a * X), label=str(a))

labelLines(ax.get_lines(), zorder=2.5)

ax = axes[1]
for a in A:
    ax.plot(X, np.sin(a * X), label=str(a))

labelLines(ax.get_lines(), align=False, fontsize=14)

ax = axes[2]
for a in A:
    ax.plot(X, loglaplace(4).pdf(a * X), label=str(a))

xvals = [0.8, 0.55, 0.22, 0.104, 0.045]
labelLines(ax.get_lines(), align=False, xvals=xvals, color="k")

ax = axes[3]
for a in A:
    ax.plot(X, chi2(5).pdf(a * X), label=str(a))

lines = ax.get_lines()
l1 = lines[-1]
labelLine(
    l1,
    0.6,
    label=r"$Re=${}".format(l1.get_label()),
    ha="left",
    va="bottom",
    align=False,
    backgroundcolor="none",
)
labelLines(lines[:-1], yoffsets=0.01, align=False, backgroundcolor="none")

# labelLines also supports log-scaled x-axes
ax = axes[4]
for a in A:
    ax.semilogx(X, np.arctan(5 * a * X), label=str(a))

labelLines(ax.get_lines(), zorder=2.5)

ax = axes[5]
for a in A:
    ax.semilogx(X, chi2(5).pdf(a * X), label=str(a))

labelLines(ax.get_lines(), xvals=(0.1, 1), zorder=2.5)

fig.show()

Example

Citing

If you're using this package for research purposes, consider citing the Zenodo entry (https://zenodo.org/record/7428071).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matplotlib_label_lines-0.6.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matplotlib_label_lines-0.6.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file matplotlib_label_lines-0.6.0.tar.gz.

File metadata

  • Download URL: matplotlib_label_lines-0.6.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for matplotlib_label_lines-0.6.0.tar.gz
Algorithm Hash digest
SHA256 2f764864bc9703f7299fec4d706b9d6dc48b25fe1369898bfbdeddb51413c82b
MD5 82031927630b12d3951c930c66d8265c
BLAKE2b-256 9e4699b6d2f3477e58fc028a29178866d8d301edd2cea20b56023083a0bd621e

See more details on using hashes here.

File details

Details for the file matplotlib_label_lines-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for matplotlib_label_lines-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3d97b236e590b17e070814652cb1da3617485b82ccb95c9d73c981d92122d3
MD5 6801b7fb872597fb270bcb2ae7f361d7
BLAKE2b-256 28ce6f5a00d9f82fb97cc78d9ef243bf698a3a9e65e8e3aab93ea0c07b3c951c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page