r""" Apply the plot protocol to an arbitrary object. Dispatches ``ob`` to the most specific plot-protocol handler available, in this order: 1. ``ob._plot_(opt_dict)`` if ``ob`` exposes a ``_plot_`` method. 2. ``plot_function_table[type(ob)](ob, opt_dict)`` if ``type(ob)``
(ob, opt_dict, xlims)
| 102 | |
| 103 | |
| 104 | def plot_protocol_apply(ob, opt_dict, xlims): |
| 105 | r""" |
| 106 | Apply the plot protocol to an arbitrary object. |
| 107 | |
| 108 | Dispatches ``ob`` to the most specific plot-protocol handler |
| 109 | available, in this order: |
| 110 | |
| 111 | 1. ``ob._plot_(opt_dict)`` if ``ob`` exposes a ``_plot_`` method. |
| 112 | 2. ``plot_function_table[type(ob)](ob, opt_dict)`` if ``type(ob)`` |
| 113 | is registered in :data:`plot_function_table`. |
| 114 | 3. If ``ob`` is callable, sample it on a uniform grid of |
| 115 | ``n_points`` (default 100) points spanning ``x_window`` (taken |
| 116 | from ``opt_dict`` or ``xlims()`` as a fallback). |
| 117 | 4. Otherwise, treat ``ob`` as an iterable of ``(x, y)`` pairs. |
| 118 | |
| 119 | Complex-valued ordinates are split into separate real and imaginary |
| 120 | curves labelled ``"Re <name>"`` and ``"Im <name>"``. |
| 121 | |
| 122 | Parameters |
| 123 | ---------- |
| 124 | ob : object |
| 125 | Object to be plotted. Must implement the plot protocol, be |
| 126 | registered in :data:`plot_function_table`, be callable, or be |
| 127 | an iterable of ``(x, y)`` pairs. |
| 128 | opt_dict : dict |
| 129 | Plotting options. Keys consumed here (``n_points``, |
| 130 | ``x_window``, ``name``) are popped; remaining keys are |
| 131 | forwarded inside the returned curve dictionaries. |
| 132 | xlims : callable |
| 133 | Zero-argument callable returning ``(xmin, xmax)`` for the |
| 134 | sampling window when ``ob`` is callable and no ``x_window`` |
| 135 | was given. Typically ``matplotlib.pyplot.xlim``. |
| 136 | |
| 137 | Returns |
| 138 | ------- |
| 139 | list of dict |
| 140 | Curve dictionaries as described in the module docstring. |
| 141 | |
| 142 | Raises |
| 143 | ------ |
| 144 | RuntimeError |
| 145 | If ``ob`` does not match any of the supported forms. |
| 146 | """ |
| 147 | |
| 148 | # the object can have a native plot function defined in the class |
| 149 | if hasattr(ob, '_plot_'): |
| 150 | return ob._plot_(opt_dict) |
| 151 | # or registered in the plot_function_table variable |
| 152 | elif type(ob) in plot_function_table: |
| 153 | return plot_function_table[type(ob)](ob, opt_dict) |
| 154 | elif callable(ob): |
| 155 | n_points = opt_dict.pop('n_points', 100) |
| 156 | rx = opt_dict.pop('x_window', None) |
| 157 | xmin, xmax = rx if rx else xlims() |
| 158 | X = numpy.arange(xmin, xmax, (xmax - xmin) / float(n_points)) |
| 159 | Y = numpy.array([ob(x) for x in X]) |
| 160 | else: |
| 161 | try: # generator x,y |