Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Integrate Matplotlib #337

Merged
merged 18 commits into from
Mar 4, 2025
Merged

feat: Integrate Matplotlib #337

merged 18 commits into from
Mar 4, 2025

Conversation

hpohekar
Copy link
Collaborator

@hpohekar hpohekar commented Feb 26, 2025

closes #132

Basic usage

import matplotlib.pyplot as plt
from ansys.units import Quantity

x_quantity = Quantity([1, 2, 3], "m")
y_quantity = Quantity([4, 5, 6], "kg")
fig, ax = plt.subplots()
ax.plot(x_quantity, y_quantity)
plt.show()

image

Example usage with numpy arrays

import matplotlib.pyplot as plt
import numpy as np
from ansys.units import Quantity, UnitRegistry

ureg = UnitRegistry()

y = Quantity(value=np.linspace(0, 30), units=ureg.m)
x = Quantity(value=np.linspace(0, 5), units=ureg.kg)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
plt.show()

image

Example usage with numpy arrays and no units

import matplotlib.pyplot as plt
import numpy as np
from ansys.units import Quantity, UnitRegistry

ureg = UnitRegistry()

y = Quantity(value=np.linspace(0, 30))
x = Quantity(value=np.linspace(0, 5))
fig, ax = plt.subplots()
ax.plot(x, y)
ax.axhline(Quantity(10, ureg.m).value, color="tab:red")
ax.axvline(Quantity(2, ureg.kg).value, color="tab:green")
plt.show()

image

@hpohekar hpohekar requested a review from a team as a code owner February 26, 2025 10:42
@github-actions github-actions bot added documentation Improvements or additions to documentation enhancement New features or code improvements labels Feb 26, 2025
@github-actions github-actions bot added the maintenance Package and maintenance related label Feb 26, 2025
@seanpearsonuk
Copy link
Collaborator

seanpearsonuk commented Feb 26, 2025

@hpohekar It appears that the PR introduces a new custom QuantityPlotter instead of making Quantity objects natively plottable.

Matplotlib will not automatically recognize Quantity objects when passed to plot().

Note that matplotlib has a built-in way of handling units (via matplotlib.units. See matplotlib.units.ConversionInterface).

@hpohekar
Copy link
Collaborator Author

@hpohekar It appears that the PR introduces a new custom QuantityPlotter instead of making Quantity objects natively plottable.

Matplotlib will not automatically recognize Quantity objects when passed to plot().

Note that matplotlib has a built-in way of handling units (via matplotlib.units. See matplotlib.units.ConversionInterface).

@seanpearsonuk

Yes, understood. I will push the changes soon. Thank you.

q1 = Quantity([1, 2, 3], 'm')
q2 = Quantity([4, 5, 6], 'kg')

import matplotlib.pyplot as plt

plt.plot(q1, q2)
plt.show()

image

@seanpearsonuk seanpearsonuk marked this pull request as draft February 27, 2025 09:32
@github-actions github-actions bot added the testing Anything related to tests label Mar 4, 2025
@hpohekar hpohekar marked this pull request as ready for review March 4, 2025 07:36
@hpohekar hpohekar merged commit b086e09 into main Mar 4, 2025
28 checks passed
@hpohekar hpohekar deleted the feat/matplotlib_integration branch March 4, 2025 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New features or code improvements maintenance Package and maintenance related testing Anything related to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

matplotlib integration
4 participants