matplotlib-label-lines example

From https://github.com/cphyc/matplotlib-label-lines.


In [ ]:
!pip install matplotlib-label-lines


Collecting matplotlib-label-lines
  Downloading https://files.pythonhosted.org/packages/68/e8/2cf7387e0cf87316d18d100cd766a14ddbd674423671035b5deeba767e8f/matplotlib_label_lines-0.3.4-py3-none-any.whl
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from matplotlib-label-lines) (3.0.3)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from matplotlib-label-lines) (1.14.6)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->matplotlib-label-lines) (1.0.1)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->matplotlib-label-lines) (2.5.3)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->matplotlib-label-lines) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->matplotlib-label-lines) (2.3.1)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from kiwisolver>=1.0.1->matplotlib->matplotlib-label-lines) (40.9.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.1->matplotlib->matplotlib-label-lines) (1.11.0)
Installing collected packages: matplotlib-label-lines
Successfully installed matplotlib-label-lines-0.3.4

In [1]:
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import loglaplace,chi2

from labellines import labelLine, labelLines

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

In [3]:
for a in A:
    plt.plot(X, np.arctan(a*X), label=str(a))

labelLines(plt.gca().get_lines(), zorder=2.5)



In [4]:
for a in A:
    plt.plot(X, np.sin(a*X), label=str(a))

labelLines(plt.gca().get_lines(), align=False, fontsize=14)



In [5]:
for a in A:
    plt.plot(X, loglaplace(4).pdf(a*X), label=str(a))

xvals = [0.8, 0.55, 0.22, 0.104, 0.045]
labelLines(plt.gca().get_lines(), align=False, xvals=xvals, color='k')



In [6]:
for a in A:
    plt.plot(X, chi2(5).pdf(a*X), label=str(a))

lines = plt.gca().get_lines()
l1=lines[-1]
labelLine(l1, 0.6, label=r'$Re=${}'.format(l1.get_label()), ha='left',
          va='bottom', align=False)
labelLines(lines[:-1], align=False)



In [7]:
for a in A:
    plt.semilogx(X, np.arctan(5*a*X), label=str(a))

labelLines(plt.gca().get_lines(), zorder=2.5)



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

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



In [11]:
fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True, figsize=(14, 4))

for j, ax in enumerate(axes):
    fig.sca(ax)
    for i in range(1, 5):
        plt.plot(X, X**(-2)/i, label='$x^2/%s$' % i)
    plt.ylim(0, 10)

    lines = []
    for i in range(-8, 12, 2):
        lines.extend(plt.plot(X, 10*X + i, '--', color='gray', label='i=%s' % i))

    if j == 0:
        labelLines(lines, zorder=2.5)
        plt.title('$\mathtt{drop\_label=False}$')
    else:
        labelLines(lines, zorder=2.5, drop_label=True)
        plt.title('$\mathtt{drop\_label=True}$')
    plt.legend()


/home/ccc/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: RuntimeWarning: divide by zero encountered in power
  

In [ ]: