|
| 1 | +""" |
| 2 | +The Axes, Axis, and Frame classes for specifying the frame. |
| 3 | +""" |
| 4 | + |
| 5 | +import dataclasses |
| 6 | + |
| 7 | +from pygmt.alias import Alias |
| 8 | +from pygmt.exceptions import GMTParameterError |
| 9 | +from pygmt.params.base import BaseParam |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass(repr=False) |
| 13 | +class Axis(BaseParam): |
| 14 | + """ |
| 15 | + Class for setting up one axis of a plot. |
| 16 | + """ |
| 17 | + |
| 18 | + # Specify the intervals for annotations, ticks, and gridlines. |
| 19 | + annot: float | None = None |
| 20 | + tick: float | None = None |
| 21 | + grid: float | None = None |
| 22 | + # The label for the axis [Default is no label]. |
| 23 | + label: str | None = None |
| 24 | + |
| 25 | + @property |
| 26 | + def _aliases(self): |
| 27 | + return [ |
| 28 | + Alias(self.annot, name="annot", prefix="a"), |
| 29 | + Alias(self.tick, name="tick", prefix="f"), |
| 30 | + Alias(self.grid, name="grid", prefix="g"), |
| 31 | + Alias(self.label, name="label", prefix="+l"), |
| 32 | + ] |
| 33 | + |
| 34 | + |
| 35 | +@dataclasses.dataclass(repr=False) |
| 36 | +class _Axes(BaseParam): |
| 37 | + """ |
| 38 | + A private class to build the Axes part of the Frame class. |
| 39 | + """ |
| 40 | + |
| 41 | + # Refer to the Frame class for full documentation. |
| 42 | + axes: str | None = None |
| 43 | + title: str | None = None |
| 44 | + |
| 45 | + @property |
| 46 | + def _aliases(self): |
| 47 | + return [ |
| 48 | + Alias(self.axes, name="axes"), |
| 49 | + Alias(self.title, name="title", prefix="+t"), |
| 50 | + ] |
| 51 | + |
| 52 | + |
| 53 | +@dataclasses.dataclass(repr=False) |
| 54 | +class Frame(BaseParam): |
| 55 | + """ |
| 56 | + Class for setting up the frame of a plot. |
| 57 | + """ |
| 58 | + |
| 59 | + #: Specify which axes to draw and their attributes. |
| 60 | + axes: str | None = None |
| 61 | + |
| 62 | + #: The title string centered above the plot frame [Default is no title]. |
| 63 | + title: str | None = None |
| 64 | + |
| 65 | + #: Specify the attributes for axes. |
| 66 | + #: |
| 67 | + #: The attributes for each axis can be specified in two ways: |
| 68 | + #: #. specifying the same attributes for all axes using the ``axis`` parameter |
| 69 | + #: #. specifying the attributes for each axis separately using the ``xaxis``, |
| 70 | + #: ``yaxis``, ``zaxis`` parameter for the x-, y, and z-axes, respectively. |
| 71 | + #: |
| 72 | + #: GMT uses the notion of primary (the default) and secondary axes, while secondary |
| 73 | + #: axes are optional and mostly used for time axes annotations. To specify the |
| 74 | + #: attributes for the secondary axes, use the ``xaxis2``, ``yaxis2``, and ``zaxis2`` |
| 75 | + #: parameters. |
| 76 | + axis: Axis | None = None |
| 77 | + xaxis: Axis | None = None |
| 78 | + yaxis: Axis | None = None |
| 79 | + zaxis: Axis | None = None |
| 80 | + xaxis2: Axis | None = None |
| 81 | + yaxis2: Axis | None = None |
| 82 | + zaxis2: Axis | None = None |
| 83 | + |
| 84 | + def _validate(self): |
| 85 | + """ |
| 86 | + Validate the parameters of the Frame class. |
| 87 | + """ |
| 88 | + if self.axis is not None and any( |
| 89 | + [self.xaxis, self.yaxis, self.zaxis, self.xaxis2, self.yaxis2, self.zaxis2] |
| 90 | + ): |
| 91 | + raise GMTParameterError( |
| 92 | + conflicts_with=( |
| 93 | + "axis", |
| 94 | + ["xaxis", "yaxis", "zaxis", "xaxis2", "yaxis2", "zaxis2"], |
| 95 | + ), |
| 96 | + reason="Either 'axis' or the individual axis parameters can be set, but not both.", |
| 97 | + ) |
| 98 | + |
| 99 | + @property |
| 100 | + def _aliases(self): |
| 101 | + return [ |
| 102 | + Alias(_Axes(axes=self.axes, title=self.title)), |
| 103 | + Alias(self.axis, name="axis"), |
| 104 | + Alias(self.xaxis, name="xaxis", prefix="px" if self.xaxis2 else "x"), |
| 105 | + Alias(self.yaxis, name="yaxis", prefix="py" if self.yaxis2 else "y"), |
| 106 | + Alias(self.zaxis, name="zaxis", prefix="pz" if self.zaxis2 else "z"), |
| 107 | + Alias(self.xaxis2, name="xaxis2", prefix="sx"), |
| 108 | + Alias(self.yaxis2, name="yaxis2", prefix="sy"), |
| 109 | + Alias(self.zaxis2, name="zaxis2", prefix="sz"), |
| 110 | + ] |
| 111 | + |
| 112 | + def __iter__(self): |
| 113 | + """ |
| 114 | + Iterate over the aliases of the class. |
| 115 | +
|
| 116 | + Yields |
| 117 | + ------ |
| 118 | + The value of each alias in the class. None are excluded. |
| 119 | + """ |
| 120 | + yield from (alias._value for alias in self._aliases if alias._value is not None) |
0 commit comments