/
stereographic_plot.py
971 lines (850 loc) · 32.4 KB
/
stereographic_plot.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# -*- coding: utf-8 -*-
# Copyright 2018-2024 the orix developers
#
# This file is part of orix.
#
# orix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# orix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with orix. If not, see <http://www.gnu.org/licenses/>.
"""Stereographic plot inheriting from :class:`~matplotlib.axes.Axes` for
plotting :class:`~orix.vector.Vector3d`.
"""
from copy import deepcopy
from typing import Any, List, Optional, Tuple, Union
from matplotlib import rcParams
import matplotlib.axes as maxes
import matplotlib.collections as mcollections
import matplotlib.patches as mpatches
import matplotlib.path as mpath
import matplotlib.projections as mprojections
import matplotlib.pyplot as plt
import numpy as np
# fmt: off
# isort: off
from orix.measure import pole_density_function
from orix.plot._symmetry_marker import (
TwoFoldMarker,
ThreeFoldMarker,
FourFoldMarker,
SixFoldMarker,
)
# isort: on
# fmt: on
from orix.projections import InverseStereographicProjection, StereographicProjection
from orix.vector import FundamentalSector, Vector3d
from orix.vector.fundamental_sector import _closed_edges_in_hemisphere
# This order determines which parts are plotted over other parts
ZORDER = dict(
text=6,
scatter=5,
symmetry_marker=4,
draw_circle=3,
border=2,
grid=1,
mesh=0,
)
class StereographicPlot(maxes.Axes):
"""Stereographic plot for plotting :class:`~orix.vector.Vector3d`.
Parameters
----------
*args
Arguments passed to :class:`matplotlib.axes.Axes`.
hemisphere
Which hemisphere to plot vectors in, either ``"upper"``
(default) or ``"lower"``.
azimuth_resolution
Resolution of azimuth grid lines in degrees. Default is 10
degrees.
polar_resolution
Resolution of polar grid lines in degrees. Default is 10
degrees.
**kwargs
Keyword arguments passed to :class:`matplotlib.axes.Axes`.
"""
name = "stereographic"
_pad_xy = 0.05
def __init__(
self,
*args,
hemisphere: str = "upper",
azimuth_resolution: Union[int, float] = 10,
polar_resolution: Union[int, float] = 10,
**kwargs,
):
"""Create an axis for plotting :class:`~orix.vector.Vector3d`."""
self.hemisphere = hemisphere
self._azimuth_resolution = azimuth_resolution
self._polar_resolution = polar_resolution
# Custom attribute to keep track of whether grid is on or off
self._stereographic_grid = None
super().__init__(*args, **kwargs)
# Set ratio of y-unit to x-unit by adjusting the physical
# dimension of the Axes (box), and centering the anchor (C)
self.set_aspect("equal", adjustable="box", anchor="C")
self.clear()
def clear(self):
super().clear()
self.xaxis.set_ticks_position("none")
self.yaxis.set_ticks_position("none")
self.xaxis.set_tick_params(label1On=False)
self.yaxis.set_tick_params(label1On=False)
self.set_xlim(-1 - self._pad_xy, 1 + self._pad_xy)
self.set_ylim(-1 - self._pad_xy, 1 + self._pad_xy)
spines = self.spines
for spine in spines.values():
spine.set_visible(False)
self.add_patch(
mpatches.Circle(
xy=(0, 0),
radius=1,
facecolor="none",
edgecolor="k",
label="sa_circle",
zorder=ZORDER["border"],
)
)
# Don't show rectangular grid
self.grid(False)
self.stereographic_grid(rcParams["axes.grid"])
def format_coord(self, x, y):
if np.sqrt(np.sum(np.square([x, y]))) > 1:
return ""
else:
azimuth, polar = self._inverse_projection.xy2spherical(x, y)
azimuth = azimuth[0]
polar = polar[0]
azimuth_deg = np.rad2deg(azimuth)
polar_deg = np.rad2deg(polar)
return (
"\N{GREEK SMALL LETTER PHI}={:.2f}\N{GREEK SMALL LETTER PI} "
"({:.2f}\N{DEGREE SIGN}), "
"\N{GREEK SMALL LETTER theta}={:.2f}\N{GREEK SMALL LETTER PI} "
"({:.2f}\N{DEGREE SIGN})"
).format(azimuth / np.pi, azimuth_deg, polar / np.pi, polar_deg)
def plot(
self,
*args: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
**kwargs,
):
"""Draw straight lines between vectors.
This method overwrites :meth:`matplotlib.axes.Axes.plot`, see
that method's docstring for parameters.
Parameters
----------
*args
Vector(s), or azimuth and polar angles, the latter two
passed as separate arguments (not keyword arguments).
**kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.plot`.
See Also
--------
matplotlib.axes.Axes.plot
"""
new_kwargs = dict(clip_on=True, linewidth=2, color="k", linestyle="-")
x, y, _, updated_kwargs = self._prepare_to_call_inherited_method(
args, kwargs, new_kwargs, sort=True
)
if x.size == 0:
return
super().plot(x, y, **updated_kwargs)
def scatter(
self,
*args: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
**kwargs,
):
"""A scatter plot of vectors.
This method overwrites :meth:`matplotlib.axes.Axes.scatter`, see
that method's docstring for parameters.
Parameters
----------
*args
Vector(s), or azimuth and polar angles, the latter two
passed as separate arguments (not keyword arguments).
**kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.scatter`.
See Also
--------
matplotlib.axes.Axes.scatter
"""
new_kwargs = dict(zorder=ZORDER["scatter"], clip_on=False)
out = self._prepare_to_call_inherited_method(args, kwargs, new_kwargs)
x, y, visible, updated_kwargs = out
if x.size == 0:
return
# Color(s)
if "color" in updated_kwargs.keys():
key_color = "color"
else:
key_color = "c"
c = updated_kwargs.pop(key_color, "C0")
c = _get_array_of_values(value=c, visible=visible)
# Size(s)
if "sizes" in updated_kwargs.keys():
key_size = "sizes"
else:
key_size = "s"
s = updated_kwargs.pop(key_size, None)
if s is not None:
s = _get_array_of_values(value=s, visible=visible)
super().scatter(x, y, c=c, s=s, **updated_kwargs)
def text(
self,
*args: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
offset: Optional[Tuple[float, float]] = None,
**kwargs,
):
"""Add text to the axes.
This method overwrites :meth:`matplotlib.axes.Axes.text`, see
that method's docstring for parameters.
Parameters
----------
*args
Vector(s), or azimuth and polar angles, the latter two
passed as separate arguments (not keyword arguments).
offset
Tuple of offsets in stereographic coordinates (X, Y). No
offset is applied if not given.
**kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.text`.
See Also
--------
matplotlib.axes.Axes.text
"""
new_kwargs = dict(va="bottom", ha="center", zorder=ZORDER["text"])
out = self._prepare_to_call_inherited_method(
args, kwargs, new_kwargs, offset=offset
)
x, y, _, updated_kwargs = out
if x.size == 0:
return
super().text(x, y, **updated_kwargs)
# ----------- Custom attributes and methods below here ----------- #
@property
def hemisphere(self) -> str:
"""Return or set the hemisphere to plot, either ``"upper"`` or
``"lower"``.
:attr:`pole` is derived from this attribute.
Parameters
----------
value : str
Either ``"upper"`` or ``"lower"``.
"""
return self._hemisphere
@hemisphere.setter
def hemisphere(self, value: str):
"""Set hemisphere to plot."""
value = value.lower()
if value in ["upper", "lower"]:
self._hemisphere = value
else:
raise ValueError(f"Hemisphere must be 'upper' or 'lower', not {value}.")
@property
def pole(self) -> int:
"""Return the projection pole, either -1 or 1, where -1 (1)
means the projection point of the stereographic transform is the
lower (upper) pole [00-1] ([001]), i.e. only vectors with z > 0
(z < 0) are plotted.
Derived from :attr:`hemisphere`.
"""
return {"upper": -1, "lower": 1}[self.hemisphere]
@property
def _projection(self):
return StereographicProjection(self.pole)
@property
def _inverse_projection(self):
return InverseStereographicProjection(self.pole)
def pole_density_function(
self,
*args: Union[np.ndarray, Vector3d],
resolution: float = 1,
sigma: float = 5,
log: bool = False,
colorbar: bool = True,
weights: Optional[np.ndarray] = None,
**kwargs: Any,
):
"""Compute the Pole Density Function (PDF) of vectors in the
stereographic projection. See :cite:`rohrer2004distribution`.
Parameters
----------
*args
Vector(s), or azimuth and polar angles of the vectors, the
latter passed as two separate arguments.
resolution
The angular resolution of the sampling grid in degrees.
Default value is 1.
sigma
The angular resolution of the applied broadening in degrees.
Default value is 5.
log
If ``True`` the log(PDF) is calculated. Default is ``True``.
colorbar
If ``True`` a colorbar is shown alongside the PDF plot.
Default is ``True``.
weights
The weights for the individual vectors. If not given, the
weight of each vector is 1.
**kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.pcolormesh`.
See Also
--------
orix.measure.pole_density_function
orix.plot.InversePoleFigurePlot.pole_density_function
orix.vector.Vector3d.pole_density_function
"""
hist, (x, y) = pole_density_function(
*args,
resolution=resolution,
sigma=sigma,
log=log,
hemisphere=self.hemisphere,
weights=weights,
)
new_kwargs = dict(zorder=ZORDER["mesh"], clip_on=True)
updated_kwargs = {**kwargs, **new_kwargs}
# plot mesh
updated_kwargs.setdefault("cmap", "magma")
# mpl.QuadMesh handles masked values by default
pc = self.pcolormesh(x, y, hist, **updated_kwargs)
if colorbar:
label = "MRD"
if log:
label = f"log({label})"
plt.colorbar(pc, label=label, ax=self)
def draw_circle(
self,
*args: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
opening_angle: Union[float, np.ndarray] = np.pi / 2,
steps: int = 100,
reproject: bool = False,
reproject_plot_kwargs: Optional[dict] = None,
**kwargs,
):
r"""Draw great or small circles with a given `opening_angle` to
one or multiple vectors.
A vector must be present in the current hemisphere for its
circle to be drawn.
Parameters
----------
args
Vector(s), or azimuth and polar angles defining vectors, the
latter two passed as separate arguments (not keyword
arguments). Circles are drawn perpendicular to these with a
given ``opening_angle``.
opening_angle
Opening angle(s) around the vector(s). Default is
:math:`\pi/2`, giving a great circle. If an array is passed,
its size must be equal to the number of circles to draw.
steps
Number of vectors to describe each circle, default is 100.
reproject
Whether to reproject parts of the circle(s) visible on the
other hemisphere. Re-projection is achieved by reflection of
the circle(s) parts located on the other hemisphere in the
projection plane. Ignored if ``hemisphere`` is ``"both"``.
Default is ``False``.
reproject_plot_kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.plot` to alter the appearance of
parts of the circle(s) visible on the other hemisphere if
``reproject=True``. These lines are dashed by default.
Values used for circle(s) on the current hemisphere are used
unless values are passed here.
**kwargs
Keyword arguments passed to
:meth:`matplotlib.axes.Axes.plot` to alter the circles'
appearance.
See Also
--------
orix.vector.Vector3d.get_circle
"""
out = self._prepare_to_call_inherited_method(args, kwargs)
x, y, visible, updated_kwargs = out
if x.size == 0:
return
if isinstance(opening_angle, np.ndarray) and opening_angle.size == visible.size:
opening_angle = opening_angle[visible]
# Get set of `steps` vectors delineating a circle per vector
v = self._inverse_projection.xy2vector(x, y)
circles = v.get_circle(opening_angle=opening_angle, steps=steps).unit
# Enable using one color per circle
color = kwargs.pop("color", "C0")
color2 = _get_array_of_values(value=color, visible=visible)
# Set above which plotting elements circles will appear (zorder)
new_kwargs = dict(zorder=ZORDER["draw_circle"], clip_on=True)
for k, v in new_kwargs.items():
kwargs.setdefault(k, v)
for i, c in enumerate(circles):
self.plot(c.azimuth, c.polar, color=color2[i], **kwargs)
if reproject:
if reproject_plot_kwargs is None:
reproject_plot_kwargs = {}
reproject_plot_kwargs.setdefault("linestyle", "--")
# Simulate reflection about the projection plane by
# temporarily setting the hemisphere to the other one
other_hemisphere = {"upper": "lower", "lower": "upper"}
self._hemisphere = other_hemisphere[self._hemisphere]
for i, c in enumerate(circles):
self.plot(c.azimuth, c.polar, color=color2[i], **reproject_plot_kwargs)
self._hemisphere = other_hemisphere[self._hemisphere]
def restrict_to_sector(
self,
sector: FundamentalSector,
pad: float = 1.3,
show_edges: bool = True,
**kwargs,
):
"""Restrict the stereographic axis to a
:class:`~orix.vector.FundamentalSector`, typically obtained from
:attr:`~orix.quaternion.Symmetry.fundamental_sector`.
Parameters
----------
sector
Fundamental sector with edges delineating a fundamental
sector.
pad
Padding outside the sector in the stereographic projection
in degrees. Default is 1.3 degrees.
show_edges
Whether to draw the sector edges. Default is ``True``.
**kwargs
Keyword arguments passed to
:class:`matplotlib.patches.PathPatch` if
``show_edges=True``.
"""
original_pole = deepcopy(sector._pole)
sector._pole = self.pole
edges = sector.edges
if edges.size == 0:
return
edges = _closed_edges_in_hemisphere(edges, sector, pole=self.pole)
sector._pole = original_pole
if edges.size == 0:
return
x, y, _ = self._pretransform_input((edges,))
pad_angle = np.deg2rad(pad)
verts = sector.vertices
center = sector.center
verts_normal = center.cross(verts)
verts_rot = verts.rotate(verts_normal, pad_angle)
x_pad, y_pad = self._projection.vector2xy(verts_rot)
pad_min = 0.01
if x_pad.size == 0:
x_min, x_max = np.min(x) - pad_min, np.max(x) + pad_min
y_min, y_max = np.min(y) - pad_min, np.max(y) + pad_min
else:
x_min = min([np.min(x) - pad_min, np.min(x_pad)])
x_max = max([np.max(x) + pad_min, np.max(x_pad)])
y_min = min([np.min(y) - pad_min, np.min(y_pad)])
y_max = max([np.max(y) + pad_min, np.max(y_pad)])
self.set(xlim=(x_min, x_max), ylim=(y_min, y_max))
self.patches[0].set_visible(False)
if show_edges:
for k, v in [("facecolor", "none"), ("edgecolor", "k"), ("linewidth", 1)]:
kwargs.setdefault(k, v)
patch = mpatches.PathPatch(
mpath.Path(np.column_stack([x, y]), closed=True),
label="sa_sector",
**kwargs,
)
self.add_patch(patch)
self.set_clip_path(patch)
labels = ["sa_azimuth_grid", "sa_polar_grid"]
for c in self.collections:
if c.get_label() in labels:
c.set_clip_path(patch)
def show_hemisphere_label(self, **kwargs):
"""Add a hemisphere label (``"upper"``/``"lower"``) to the upper
left of the plot.
Parameters
----------
**kwargs
Keyword arguments passed to
:func:`matplotlib.axes.Axes.text`.
See Also
--------
hemisphere
"""
new_kwargs = dict(ha="right", va="bottom")
new_kwargs.update(kwargs)
super().text(-0.71, 0.71, s=self.hemisphere, **new_kwargs)
def set_labels(
self,
xlabel: Union[str, bool, None] = "x",
ylabel: Union[str, bool, None] = "y",
zlabel: Union[str, bool, None] = "z",
**kwargs,
):
"""Set the reference frame's axes labels.
Parameters
----------
xlabel
X axis label, default is ``"x"``. If ``False`` or ``None``,
this label is not shown.
ylabel
Y axis label, default is ``"y"``. If ``False`` or ``None``,
this label is not shown.
zlabel
Z axis label, default is ``"z"``. If ``False`` or ``None``,
this label is not shown.
"""
pos = [(1, 0), (0, 1), (0, 0)]
for (x, y), label in zip(pos, [xlabel, ylabel, zlabel]):
if label not in [None, False]:
self._set_label(x=x, y=y, label=label, **kwargs)
def stereographic_grid(
self,
show_grid: Optional[bool] = None,
azimuth_resolution: Optional[float] = None,
polar_resolution: Optional[float] = None,
):
"""Turn a stereographic grid on or off, and set the azimuth and
polar grid resolution in degrees.
Parameters
----------
show_grid
Whether to show grid lines. If any keyword arguments are
passed, this is set to ``True``. If not given and there are
no keyword arguments, the grid lines are toggled.
azimuth_resolution
Azimuth grid resolution in degrees. Default is 10 degrees.
This can also be set upon initialization of the axes by
passing ``azimuth_resolution`` to ``subplot_kw``.
polar_resolution
Polar grid resolution in degrees. Default is 10 degrees.
This can also be set upon initialization of the axes by
passing ``polar_resolution`` to ``subplot_kw``.
See Also
--------
matplotlib.axes.Axes.grid
"""
if (
show_grid is None
and self._stereographic_grid in [None, False]
or show_grid is None
and (azimuth_resolution is not None or polar_resolution is not None)
or show_grid is True
) and hasattr(self, "patch"):
self._azimuth_grid(azimuth_resolution)
self._polar_grid(polar_resolution)
self._stereographic_grid = True
elif show_grid in [None, False] and self._stereographic_grid is True:
# Remove grid
has_azimuth, index_azimuth = self._has_collection(
"sa_azimuth_grid", self.collections
)
has_polar, index_polar = self._has_collection(
"sa_polar_grid", self.collections
)
if has_azimuth:
if index_polar > index_azimuth:
index_polar -= 1
self.collections[index_azimuth].remove()
if has_polar:
self.collections[index_polar].remove()
self._stereographic_grid = False
def symmetry_marker(self, v: Vector3d, fold: int, **kwargs):
"""Plot 2-, 3- 4- or 6-fold symmetry marker(s).
Parameters
----------
v
Position of the marker(s) to plot.
fold
Which symmetry element to plot, can be either 2, 3, 4 or 6.
**kwargs
Keyword arguments passed to :meth:`scatter`.
"""
if fold not in [2, 3, 4, 6]:
raise ValueError("Can only plot 2-, 3-, 4- or 6-fold elements.")
marker_classes = {
"2": TwoFoldMarker,
"3": ThreeFoldMarker,
"4": FourFoldMarker,
"6": SixFoldMarker,
}
marker = marker_classes[str(fold)](v, size=kwargs.pop("s", 1))
new_kwargs = dict(zorder=ZORDER["symmetry_marker"], clip_on=False)
for k, v in new_kwargs.items():
kwargs.setdefault(k, v)
for vec, marker, marker_size in marker:
self.scatter(vec, marker=marker, s=marker_size, **kwargs)
# TODO: Find a way to control padding, so that markers aren't
# clipped
def _azimuth_grid(self, resolution: Optional[float] = None):
"""Set the azimuth grid resolution in degrees.
Parameters
----------
resolution
Azimuth grid resolution in degrees. Default is 10 degrees.
This can also be set upon initialization of the axes by
passing ``azimuth_resolution`` to ``subplot_kw``.
See Also
--------
polar_grid
matplotlib.axes.Axes.grid
"""
if resolution is not None:
self._azimuth_resolution = resolution
azimuth_start = np.arange(0, np.pi, np.radians(self._azimuth_resolution))
polar = np.full(azimuth_start.size, np.pi / 2)
if self.hemisphere == "lower":
polar += 1e-9
v_start = Vector3d.from_polar(azimuth_start, polar)
x_start, y_start = self._projection.vector2xy(v_start)
v_end = Vector3d.from_polar(azimuth_start + np.pi, polar)
x_end, y_end = self._projection.vector2xy(v_end)
kwargs = dict(
linewidths=rcParams["grid.linewidth"],
linestyle=rcParams["grid.linestyle"],
alpha=rcParams["grid.alpha"],
color=rcParams["grid.color"],
antialiased=True,
zorder=ZORDER["grid"],
)
label = "sa_azimuth_grid"
lines = np.stack(((x_start, x_end), (y_start, y_end))).T
lines_collection = mcollections.LineCollection(lines, label=label, **kwargs)
has_collection, index = self._has_collection(label, self.collections)
if has_collection:
self.collections[index].remove()
has_sector, sector_index = self._has_collection("sa_sector", self.patches)
if has_sector:
lines_collection.set_clip_path(self.patches[sector_index])
self.add_collection(lines_collection)
@staticmethod
def _has_collection(label, collections):
labels = [c.get_label() for c in collections]
for i in range(len(labels)):
if label == labels[i]:
return True, i
return False, -1
def _polar_grid(self, resolution: Optional[float] = None):
"""Set the polar grid resolution in degrees.
Parameters
----------
resolution
Polar grid resolution in degrees. Default is 15 degrees.
This can also be set upon initialization of the axes by
passing ``polar_resolution`` to ``subplot_kw``.
See Also
--------
azimuth_grid
matplotlib.axes.Axes.grid
"""
if resolution is not None:
self._polar_resolution = resolution
res = np.radians(self._polar_resolution)
polar = np.arange(res, np.pi, res)
v = Vector3d.from_polar(np.zeros(polar.size), polar)
radii, _ = self._projection.vector2xy(v)
ec = rcParams["grid.color"]
kwargs = dict(
xy=(0, 0),
linewidth=rcParams["grid.linewidth"],
linestyle=rcParams["grid.linestyle"],
alpha=rcParams["grid.alpha"],
ec=ec,
fc="none",
antialiased=True,
zorder=ZORDER["grid"],
)
circles = []
for r in radii:
circles.append(mpatches.Circle(radius=r, **kwargs))
label = "sa_polar_grid"
circles_collection = mcollections.PatchCollection(
circles,
label=label,
edgecolors=kwargs["ec"],
facecolors=kwargs["fc"],
alpha=kwargs["alpha"],
)
has_collection, index = self._has_collection(label, self.collections)
if has_collection:
self.collections[index].remove()
has_sector, sector_index = self._has_collection("sa_sector", self.patches)
if has_sector:
circles_collection.set_clip_path(self.patches[sector_index])
self.add_collection(circles_collection)
def _prepare_to_call_inherited_method(
self,
args: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
kwargs: dict,
new_kwargs: Optional[dict] = None,
sort: bool = False,
offset: Tuple[float, float] = None,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, dict]:
"""Prepare arguments and keyword arguments passed to methods in
:class:`StereographicPlot` inherited from
:class:`matplotlib.axes.Axes`.
Parameters
----------
args
Any arguments passed to the :class:`StereographicPlot`
method.
kwargs
Any arguments passed to the :class:`StereographicPlot`
method.
new_kwargs
Any default keyword arguments to be passed to the inherited
method.
sort
Whether to sort vectors before passing them to Matplotlib.
Default is ``False``.
offset
Tuple of offsets in (X, Y). No offset is applied if not
given.
Returns
-------
x
y
visible
updated_kwargs
"""
updated_kwargs = kwargs
if new_kwargs is not None:
for k, v in new_kwargs.items():
updated_kwargs.setdefault(k, v)
x, y, visible = self._pretransform_input(args, sort=sort, offset=offset)
return x, y, visible, updated_kwargs
def _pretransform_input(
self,
values: Union[Vector3d, Tuple[float, float], Tuple[np.ndarray, np.ndarray]],
sort: bool = False,
offset: Optional[Tuple[float, float]] = None,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Return arrays of (X, Y) from input data.
Parameters
----------
values
Spherical coordinates (azimuth, polar) or vectors. If
spherical coordinates are given, they are assumed to
describe unit vectors. Vectors will be made into unit
vectors if they are not already.
sort
Whether to sort vectors before passing them to Matplotlib.
Default is ``False``.
offset
Tuple of offsets in (X, Y). No offset is applied if not
given.
Returns
-------
X
Stereographic X coordinates of unit vectors.
Y
Stereographic Y coordinates of unit vectors.
visible
Whether these values are visible on the axes.
"""
pole = self.pole
if len(values) == 2:
azimuth, polar = values[0], values[1]
if sort:
order = _order_in_hemisphere(polar, pole)
azimuth = azimuth[order]
polar = polar[order]
x, y = self._projection.spherical2xy(azimuth=azimuth, polar=polar)
v = self._inverse_projection.xy2vector(x, y)
else:
try:
v = values[0].flatten().unit
if sort:
order = _order_in_hemisphere(v.polar, pole)
v = v[order]
x, y = self._projection.vector2xy(v)
except (ValueError, AttributeError):
raise ValueError(
"Accepts only one (Vector3d) or two (azimuth, polar) input "
"arguments."
)
visible = v <= self._projection.region
if offset is not None:
x += offset[0]
y += offset[1]
return x, y, visible
def _set_label(self, x: float, y: float, label: str, **kwargs):
bbox_dict = dict(boxstyle="round, pad=0.1", fc="w", ec="w")
new_kwargs = dict(ha="center", va="center", bbox=bbox_dict)
new_kwargs.update(kwargs)
super().text(x=x, y=y, s=label, **new_kwargs)
mprojections.register_projection(StereographicPlot)
def _get_array_of_values(
value: Union[str, float, List[str], List[float]], visible: np.ndarray
) -> np.ndarray:
"""Return a usable array of ``value`` with the correct size
even though ``value`` doesn't have as many elements as
``visible.size``, to be iterated over along with ``True`` elements
in ``visible``.
Parameters
----------
value
Typically a keyword argument value to be passed to some
Matplotlib routine.
visible
Boolean array with as many elements as input vectors, only some
of which are visible in the hemisphere (``True``).
Returns
-------
array
An array populated with ``value`` of a size equal to the number
of ``True`` elements in ``visible``.
"""
n = visible.size
if not isinstance(value, str) and hasattr(value, "__iter__") and len(value) != n:
value = value[0]
if isinstance(value, str) or not hasattr(value, "__iter__"):
value = [value] * n
return np.asarray(value)[visible]
def _is_visible(polar: np.ndarray, pole: int) -> np.ndarray:
"""Return a boolean array describing whether the vector which the
polar angles belong to are visible in the current hemisphere.
Parameters
----------
polar
pole
Returns
-------
polar_visible
Boolean array with ``True`` for polar angles corresponding to
vectors visible in this hemisphere.
"""
if pole == -1:
return polar <= np.pi / 2
else: # pole == 1
return polar >= np.pi / 2
def _order_in_hemisphere(polar: np.ndarray, pole: int) -> Union[np.ndarray, None]:
"""Return order of vectors based on polar angles, so that the ones
corresponding to vectors visible in this hemisphere are shifted to
the start of the arrays.
Used in :meth:`StereographicPlot._pretransform_input` when
``sort=True``.
Parameters
----------
polar
pole
Returns
-------
out
If no vectors are visible, ``None`` is returned.
"""
visible = _is_visible(polar, pole)
if visible.size == 0 or not np.any(visible):
return
indices = np.asarray(visible != visible[0]).nonzero()[0]
order = np.arange(visible.size)
if indices.size != 0:
order = np.roll(order, shift=-(indices[-1] + 1))
return order