Skip to content

Commit a610eca

Browse files
committed
feat: Integrate Matplotlib
1 parent 5e4a758 commit a610eca

File tree

11 files changed

+243
-2
lines changed

11 files changed

+243
-2
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
additional_dependencies: [black==23.7.0]
1818

1919
- repo: https://github.com/pycqa/isort
20-
rev: 5.13.2
20+
rev: 6.0.0
2121
hooks:
2222
- id: isort
2323

doc/source/_static/plot_basic.png

21.3 KB
Loading

doc/source/_static/plot_no_units.png

15.8 KB
Loading

doc/source/_static/plot_np_array.png

17.2 KB
Loading

doc/source/api/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ This section describes the public classes, functions, and attributes in the PyAn
1212
dimensions
1313
config
1414
unit
15-
unit_registry
15+
unit_registry
16+
plot

doc/source/api/plot.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. _ref_plot:
2+
3+
================
4+
Quantity Plotter
5+
================
6+
7+
.. autoclass:: ansys.units.plot.QuantityPlotter
8+
:members:
9+
:show-inheritance:
10+
:undoc-members:
11+
:exclude-members:

doc/source/user_guide/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ code to work with physical quantities.
1515
strings
1616
angles
1717
temperature
18+
plot
1819

doc/source/user_guide/plot.rst

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. _plot:
2+
3+
========================
4+
Plotting with Matplotlib
5+
========================
6+
7+
PyAnsys Units provides a simple interface to plot quantities using `Matplotlib <https://matplotlib.org/stable/index.html>`_.
8+
9+
Basic usage
10+
***********
11+
12+
The following example demonstrates how to plot data using lists.
13+
14+
.. code:: python
15+
16+
import matplotlib.pyplot as plt
17+
from ansys.units import Quantity, QuantityPlotter
18+
19+
x_quantity = Quantity([1, 2, 3], "m")
20+
y_quantity = Quantity([4, 5, 6], "kg")
21+
data = QuantityPlotter(x_quantity, y_quantity)
22+
fig, ax = plt.subplots()
23+
data.plot(ax)
24+
plt.show()
25+
26+
27+
.. image:: ../_static/plot_basic.png
28+
:alt: Basic plot example
29+
30+
31+
Using numpy arrays
32+
******************
33+
34+
The following example demonstrates how to plot data using numpy arrays.
35+
36+
.. code:: python
37+
38+
import matplotlib.pyplot as plt
39+
from ansys.units import Quantity, QuantityPlotter, UnitRegistry
40+
41+
ureg = UnitRegistry()
42+
y = Quantity(value=np.linspace(0, 30), units=ureg.m)
43+
x = Quantity(value=np.linspace(0, 5), units=ureg.kg)
44+
data = QuantityPlotter(x, y)
45+
fig, ax = plt.subplots()
46+
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
47+
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
48+
data.plot(ax)
49+
plt.show()
50+
51+
52+
.. image:: ../_static/plot_np_array.png
53+
:alt: Plot example using numpy arrays
54+
55+
56+
Using numpy arrays and no units
57+
*******************************
58+
59+
The following example demonstrates how to plot data without units.
60+
61+
.. code:: python
62+
63+
import matplotlib.pyplot as plt
64+
from ansys.units import Quantity, QuantityPlotter, UnitRegistry
65+
66+
ureg = UnitRegistry()
67+
y = Quantity(value=np.linspace(0, 30))
68+
x = Quantity(value=np.linspace(0, 5))
69+
data = QuantityPlotter(x, y)
70+
fig, ax = plt.subplots()
71+
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
72+
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
73+
data.plot(ax)
74+
plt.show()
75+
76+
.. image:: ../_static/plot_no_units.png
77+
:alt: Plot example using numpy arrays and no units
78+

examples/00-pyunits/basic_usage.py

+50
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,53 @@
307307

308308
joules = ten_N * ten_m
309309
joules # >>> Quantity (100.0, "J")
310+
311+
312+
###############################################################################
313+
# Plotting with Matplotlib
314+
# ~~~~~~~~~~~~~~~~~~~~~~~~
315+
# You can plot quantities using the QuantityPlotter class.
316+
317+
# Basic usage
318+
319+
import matplotlib.pyplot as plt
320+
import numpy as np
321+
322+
from ansys.units import Quantity, QuantityPlotter, UnitRegistry
323+
324+
ureg = UnitRegistry()
325+
326+
x_quantity = Quantity([1, 2, 3], "m")
327+
y_quantity = Quantity([4, 5, 6], "kg")
328+
data = QuantityPlotter(x_quantity, y_quantity)
329+
fig, ax = plt.subplots()
330+
data.plot(ax)
331+
plt.show()
332+
333+
# sphinx_gallery_thumbnail_path = '../_static/plot_basic.png'
334+
335+
# Example usage with numpy arrays
336+
337+
y = Quantity(value=np.linspace(0, 30), units=ureg.m)
338+
x = Quantity(value=np.linspace(0, 5), units=ureg.kg)
339+
data = QuantityPlotter(x, y)
340+
fig, ax = plt.subplots()
341+
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
342+
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
343+
data.plot(ax)
344+
plt.show()
345+
346+
# sphinx_gallery_thumbnail_path = '../_static/plot_np_array.png'
347+
348+
# Example usage with numpy arrays and no units
349+
350+
y = Quantity(value=np.linspace(0, 30))
351+
x = Quantity(value=np.linspace(0, 5))
352+
data = QuantityPlotter(x, y)
353+
fig, ax = plt.subplots()
354+
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
355+
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
356+
data.plot(ax)
357+
plt.show()
358+
359+
# sphinx_gallery_thumbnail_path = '../_static/plot_no_units.png'

src/ansys/units/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
)
4141
from ansys.units.base_dimensions import BaseDimensions # noqa: F401
4242
from ansys.units.dimensions import Dimensions # noqa: F401
43+
from ansys.units.plot import QuantityPlotter # noqa: F401
4344
from ansys.units.quantity import Quantity, get_si_value # noqa: F401
4445
from ansys.units.systems import UnitSystem # noqa: F401
4546
from ansys.units.unit import Unit # noqa: F401

src/ansys/units/plot.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)