{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Artist within an artist\n\nOverride basic methods so an artist can contain another\nartist.  In this case, the line contains a Text instance to label it.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.lines as lines\nimport matplotlib.text as mtext\nimport matplotlib.transforms as mtransforms\n\n\nclass MyLine(lines.Line2D):\n    def __init__(self, *args, **kwargs):\n        # we'll update the position when the line data is set\n        self.text = mtext.Text(0, 0, '')\n        super().__init__(*args, **kwargs)\n\n        # we can't access the label attr until *after* the line is\n        # initiated\n        self.text.set_text(self.get_label())\n\n    def set_figure(self, figure):\n        self.text.set_figure(figure)\n        super().set_figure(figure)\n\n    # Override the Axes property setter to set Axes on our children as well.\n    @lines.Line2D.axes.setter\n    def axes(self, new_axes):\n        self.text.axes = new_axes\n        lines.Line2D.axes.fset(self, new_axes)  # Call the superclass property setter.\n\n    def set_transform(self, transform):\n        # 2 pixel offset\n        texttrans = transform + mtransforms.Affine2D().translate(2, 2)\n        self.text.set_transform(texttrans)\n        super().set_transform(transform)\n\n    def set_data(self, x, y):\n        if len(x):\n            self.text.set_position((x[-1], y[-1]))\n\n        super().set_data(x, y)\n\n    def draw(self, renderer):\n        # draw my label at the end of the line with 2 pixel offset\n        super().draw(renderer)\n        self.text.draw(renderer)\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n\nfig, ax = plt.subplots()\nx, y = np.random.rand(2, 20)\nline = MyLine(x, y, mfc='red', ms=12, label='line label')\nline.text.set_color('red')\nline.text.set_fontsize(16)\n\nax.add_line(line)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.lines`\n   - `matplotlib.lines.Line2D`\n   - `matplotlib.lines.Line2D.set_data`\n   - `matplotlib.artist`\n   - `matplotlib.artist.Artist`\n   - `matplotlib.artist.Artist.draw`\n   - `matplotlib.artist.Artist.set_transform`\n   - `matplotlib.text`\n   - `matplotlib.text.Text`\n   - `matplotlib.text.Text.set_color`\n   - `matplotlib.text.Text.set_fontsize`\n   - `matplotlib.text.Text.set_position`\n   - `matplotlib.axes.Axes.add_line`\n   - `matplotlib.transforms`\n   - `matplotlib.transforms.Affine2D`\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}