|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +import matplotlib as mpl |
| 5 | +from matplotlib.axes._axes import Axes as MPLAxes, _preprocess_data |
| 6 | +import matplotlib.collections as mcoll |
| 7 | +import matplotlib.cbook as cbook |
| 8 | +import matplotlib.markers as mmarkers |
| 9 | +import matplotlib.projections as mprojections |
| 10 | + |
| 11 | +from .containers import ArrayContainer, DataUnion |
| 12 | +from .conversion_node import ( |
| 13 | + MatplotlibUnitConversion, |
| 14 | + FunctionConversionNode, |
| 15 | + RenameConversionNode, |
| 16 | +) |
| 17 | +from .wrappers import PathCollectionWrapper |
| 18 | + |
| 19 | + |
| 20 | +class Axes(MPLAxes): |
| 21 | + # Name for registering as a projection so we can experiment with it |
| 22 | + name = "data-prototype" |
| 23 | + |
| 24 | + @_preprocess_data( |
| 25 | + replace_names=[ |
| 26 | + "x", |
| 27 | + "y", |
| 28 | + "s", |
| 29 | + "linewidths", |
| 30 | + "edgecolors", |
| 31 | + "c", |
| 32 | + "facecolor", |
| 33 | + "facecolors", |
| 34 | + "color", |
| 35 | + ], |
| 36 | + label_namer="y", |
| 37 | + ) |
| 38 | + def scatter( |
| 39 | + self, |
| 40 | + x, |
| 41 | + y, |
| 42 | + s=None, |
| 43 | + c=None, |
| 44 | + marker=None, |
| 45 | + cmap=None, |
| 46 | + norm=None, |
| 47 | + vmin=None, |
| 48 | + vmax=None, |
| 49 | + alpha=None, |
| 50 | + linewidths=None, |
| 51 | + *, |
| 52 | + edgecolors=None, |
| 53 | + plotnonfinite=False, |
| 54 | + **kwargs |
| 55 | + ): |
| 56 | + # TODO implement normalize kwargs as a pipeline stage |
| 57 | + # add edgecolors and linewidths to kwargs so they can be processed by |
| 58 | + # normalize_kwargs |
| 59 | + if edgecolors is not None: |
| 60 | + kwargs.update({"edgecolors": edgecolors}) |
| 61 | + if linewidths is not None: |
| 62 | + kwargs.update({"linewidths": linewidths}) |
| 63 | + |
| 64 | + kwargs = cbook.normalize_kwargs(kwargs, mcoll.Collection) |
| 65 | + c, colors, edgecolors = self._parse_scatter_color_args( |
| 66 | + c, |
| 67 | + edgecolors, |
| 68 | + kwargs, |
| 69 | + np.ma.ravel(x).size, |
| 70 | + get_next_color_func=self._get_patches_for_fill.get_next_color, |
| 71 | + ) |
| 72 | + |
| 73 | + inputs = ArrayContainer( |
| 74 | + x=x, |
| 75 | + y=y, |
| 76 | + s=s, |
| 77 | + c=c, |
| 78 | + marker=marker, |
| 79 | + cmap=cmap, |
| 80 | + norm=norm, |
| 81 | + vmin=vmin, |
| 82 | + vmax=vmax, |
| 83 | + alpha=alpha, |
| 84 | + plotnonfinite=plotnonfinite, |
| 85 | + facecolors=colors, |
| 86 | + edgecolors=edgecolors, |
| 87 | + **kwargs |
| 88 | + ) |
| 89 | + # TODO should more go in here? |
| 90 | + # marker/s are always in Container, but require overriding if None |
| 91 | + # Color handling is odd too |
| 92 | + defaults = ArrayContainer( |
| 93 | + linewidths=mpl.rcParams["lines.linewidth"], |
| 94 | + ) |
| 95 | + |
| 96 | + cont = DataUnion(defaults, inputs) |
| 97 | + |
| 98 | + pipeline = [] |
| 99 | + xconvert = MatplotlibUnitConversion.from_keys(("x",), axis=self.xaxis) |
| 100 | + yconvert = MatplotlibUnitConversion.from_keys(("y",), axis=self.yaxis) |
| 101 | + pipeline.extend([xconvert, yconvert]) |
| 102 | + pipeline.append(lambda x: np.ma.ravel(x)) |
| 103 | + pipeline.append(lambda y: np.ma.ravel(y)) |
| 104 | + pipeline.append( |
| 105 | + lambda s: np.ma.ravel(s) |
| 106 | + if s is not None |
| 107 | + else [20] |
| 108 | + if mpl.rcParams["_internal.classic_mode"] |
| 109 | + else [mpl.rcParams["lines.markersize"] ** 2.0] |
| 110 | + ) |
| 111 | + # TODO plotnonfinite/mask combining |
| 112 | + pipeline.append( |
| 113 | + lambda marker: marker |
| 114 | + if marker is not None |
| 115 | + else mpl.rcParams["scatter.marker"] |
| 116 | + ) |
| 117 | + pipeline.append( |
| 118 | + lambda marker: marker |
| 119 | + if isinstance(marker, mmarkers.MarkerStyle) |
| 120 | + else mmarkers.MarkerStyle(marker) |
| 121 | + ) |
| 122 | + pipeline.append( |
| 123 | + FunctionConversionNode.from_funcs( |
| 124 | + { |
| 125 | + "paths": lambda marker: [ |
| 126 | + marker.get_path().transformed(marker.get_transform()) |
| 127 | + ] |
| 128 | + } |
| 129 | + ) |
| 130 | + ) |
| 131 | + pipeline.append(RenameConversionNode.from_mapping({"s": "sizes"})) |
| 132 | + |
| 133 | + # TODO classic mode margin override? |
| 134 | + pcw = PathCollectionWrapper(cont, pipeline, offset_transform=self.transData) |
| 135 | + self.add_artist(pcw) |
| 136 | + self._request_autoscale_view() |
| 137 | + return pcw |
| 138 | + |
| 139 | + |
| 140 | +# This is a handy trick to allow e.g. plt.subplots(subplot_kw={'projection': 'data-prototype'}) |
| 141 | +mprojections.register_projection(Axes) |
0 commit comments