跳转到主要内容

在matplotlib中标注线条。

项目描述

matplotlib-label-lines

Build status Supported Python Versions PyPI codecov

使用matplotlib轻松标注线条。

代码主要基于http://stackoverflow.com/questions/16992038/inline-labels-in-matplotlib(来自NauticalMile的原始代码)。

安装

只需这样做

pip install matplotlib-label-lines

您可以在binder Binder上在线尝试,从以下示例示例或以下脚本中获得一些灵感

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

引用

如果您将此软件包用于研究目的,请考虑引用Zenodo条目(https://zenodo.org/record/7428071)

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

matplotlib_label_lines-0.7.0.tar.gz (13.2 kB 查看哈希值)

上传时间 源代码

构建分发

matplotlib_label_lines-0.7.0-py3-none-any.whl (12.8 kB 查看哈希值)

上传时间 Python 3

由以下提供支持