|
| 1 | +# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +"""Plot Quantity objects with matplotlib.""" |
| 24 | + |
| 25 | +import matplotlib.pyplot as plt |
| 26 | + |
| 27 | +from ansys.units import Quantity |
| 28 | + |
| 29 | + |
| 30 | +class QuantityPlotter: |
| 31 | + """ |
| 32 | + Plot a Quantity object. |
| 33 | +
|
| 34 | + Parameters |
| 35 | + ---------- |
| 36 | + x_quantity : Quantity |
| 37 | + The x-axis quantity. |
| 38 | + y_quantity : Quantity |
| 39 | + The y-axis quantity. |
| 40 | +
|
| 41 | + Examples |
| 42 | + -------- |
| 43 | + >>> import matplotlib.pyplot as plt |
| 44 | + >>> from ansys.units import Quantity, QuantityPlotter, UnitRegistry |
| 45 | + >>> x_quantity = Quantity([1, 2, 3], 'm') |
| 46 | + >>> y_quantity = Quantity([4, 5, 6], 'kg') |
| 47 | + >>> data = QuantityPlotter(x_quantity, y_quantity) |
| 48 | + >>> fig, ax = plt.subplots() |
| 49 | + >>> data.plot(ax) |
| 50 | + >>> plt.show() |
| 51 | + >>> # Example usage with numpy arrays |
| 52 | + >>> ureg = UnitRegistry() |
| 53 | + >>> y = Quantity(value=np.linspace(0, 30), units=ureg.m) |
| 54 | + >>> x = Quantity(value=np.linspace(0, 5), units=ureg.kg) |
| 55 | + >>> data = QuantityPlotter(x, y) |
| 56 | + >>> fig, ax = plt.subplots() |
| 57 | + >>> ax.axhline(Quantity(10, ureg.m).value, color='tab:red') |
| 58 | + >>> ax.axvline(Quantity(2, ureg.kg).value, color='tab:green') |
| 59 | + >>> data.plot(ax) |
| 60 | + >>> plt.show() |
| 61 | + """ |
| 62 | + |
| 63 | + def __init__(self, x_quantity, y_quantity): |
| 64 | + if isinstance(x_quantity, Quantity): |
| 65 | + self.x_quantity_value = x_quantity.value |
| 66 | + self.x_quantity_unit = x_quantity.units._name |
| 67 | + else: |
| 68 | + self.x_quantity_value = x_quantity |
| 69 | + self.x_quantity_unit = None |
| 70 | + |
| 71 | + if isinstance(y_quantity, Quantity): |
| 72 | + self.y_quantity_value = y_quantity.value |
| 73 | + self.y_quantity_unit = y_quantity.units._name |
| 74 | + else: |
| 75 | + self.y_quantity_value = y_quantity |
| 76 | + self.y_quantity_unit = None |
| 77 | + |
| 78 | + def plot(self, ax=None, **kwargs): |
| 79 | + """ |
| 80 | + Plot the Quantity object. |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + ax : matplotlib.axes.Axes, optional |
| 85 | + The matplotlib axes to plot on. If None, the current axes will be used. |
| 86 | + **kwargs |
| 87 | + Additional keyword arguments to pass to matplotlib.pyplot.plot. |
| 88 | + """ |
| 89 | + if ax is None: |
| 90 | + ax = plt.gca() |
| 91 | + |
| 92 | + ax.plot(self.x_quantity_value, self.y_quantity_value, **kwargs) |
| 93 | + |
| 94 | + if self.x_quantity_unit: |
| 95 | + ax.set_xlabel(f"X ({self.x_quantity_unit})") |
| 96 | + if self.y_quantity_unit: |
| 97 | + ax.set_ylabel(f"Y ({self.y_quantity_unit})") |
| 98 | + |
| 99 | + return ax |
0 commit comments