- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaxes.py
147 lines (134 loc) · 4.22 KB
/
axes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
importnumpyasnp
importmatplotlibasmpl
frommatplotlib.axes._axesimportAxesasMPLAxes, _preprocess_data
importmatplotlib.collectionsasmcoll
importmatplotlib.cbookascbook
importmatplotlib.markersasmmarkers
importmatplotlib.projectionsasmprojections
from .containersimportArrayContainer, DataUnion
from .conversion_nodeimport (
DelayedConversionNode,
FunctionConversionNode,
RenameConversionNode,
)
from .wrappersimportPathCollectionWrapper
classAxes(MPLAxes):
# Name for registering as a projection so we can experiment with it
name="data-prototype"
@_preprocess_data(
replace_names=[
"x",
"y",
"s",
"linewidths",
"edgecolors",
"c",
"facecolor",
"facecolors",
"color",
],
label_namer="y",
)
defscatter(
self,
x,
y,
s=None,
c=None,
marker=None,
cmap=None,
norm=None,
vmin=None,
vmax=None,
alpha=None,
linewidths=None,
*,
edgecolors=None,
plotnonfinite=False,
**kwargs
):
# TODO implement normalize kwargs as a pipeline stage
# add edgecolors and linewidths to kwargs so they can be processed by
# normalize_kwargs
ifedgecolorsisnotNone:
kwargs.update({"edgecolors": edgecolors})
iflinewidthsisnotNone:
kwargs.update({"linewidths": linewidths})
kwargs=cbook.normalize_kwargs(kwargs, mcoll.Collection)
c, colors, edgecolors=self._parse_scatter_color_args(
c,
edgecolors,
kwargs,
np.ma.ravel(x).size,
get_next_color_func=self._get_patches_for_fill.get_next_color,
)
inputs=ArrayContainer(
x=x,
y=y,
s=s,
c=c,
marker=marker,
cmap=cmap,
norm=norm,
vmin=vmin,
vmax=vmax,
alpha=alpha,
plotnonfinite=plotnonfinite,
facecolors=colors,
edgecolors=edgecolors,
**kwargs
)
# TODO should more go in here?
# marker/s are always in Container, but require overriding if None
# Color handling is odd too
defaults=ArrayContainer(
linewidths=mpl.rcParams["lines.linewidth"],
)
cont=DataUnion(defaults, inputs)
pipeline= []
xconvert=DelayedConversionNode.from_keys(("x",), converter_key="xunits")
yconvert=DelayedConversionNode.from_keys(("y",), converter_key="yunits")
pipeline.extend([xconvert, yconvert])
pipeline.append(lambdax: np.ma.ravel(x))
pipeline.append(lambday: np.ma.ravel(y))
pipeline.append(
lambdas: (
np.ma.ravel(s)
ifsisnotNone
else (
[20]
ifmpl.rcParams["_internal.classic_mode"]
else [mpl.rcParams["lines.markersize"] **2.0]
)
)
)
# TODO plotnonfinite/mask combining
pipeline.append(
lambdamarker: (
markerifmarkerisnotNoneelsempl.rcParams["scatter.marker"]
)
)
pipeline.append(
lambdamarker: (
marker
ifisinstance(marker, mmarkers.MarkerStyle)
elsemmarkers.MarkerStyle(marker)
)
)
pipeline.append(
FunctionConversionNode.from_funcs(
{
"paths": lambdamarker: [
marker.get_path().transformed(marker.get_transform())
]
}
)
)
pipeline.append(RenameConversionNode.from_mapping({"s": "sizes"}))
# TODO classic mode margin override?
pcw=PathCollectionWrapper(cont, pipeline, offset_transform=self.transData)
self.add_artist(pcw)
self._request_autoscale_view()
returnpcw
# This is a handy trick to allow e.g. plt.subplots(subplot_kw={'projection': 'data-prototype'})
mprojections.register_projection(Axes)