You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems like matplotlib's quiver plot isn't supported in the matplotlib_support context manager when x and y have mixed units. I'm not really sure this a unyt problem (because of how quiver works), so feel free to close this, but I think it should be at least mentioned in the documentation.
Example
# Basic example from Strogatz's Nonlinear Dynamics and Chaos (Example 6.5.2)x=np.linspace(-2,2) *metery=np.linspace(-2,2), *meter/secondxg,yg=np.meshgrid(x,y)
dt=1*secondxdot=ygydot= ((1/second) *xg- (1/second/meter**2) *xg**3)
u=xdot*dtv=ydot*dt# u and x have the same units and v and y have the same units to match angles='xy', scale_units='xy'fig,ax=plt.subplots()
withmatplotlib_support:
ax.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=10)
Note the primary problem appears to be the xy column stacking (the self.xy = np.column_stack((X, Y)), but additional problems may show up if for example x and xdot have different units. This isn't surprising but a better error message may be helpful.
or using (artificially) consistent units (e.g. changing y, u, v units to meters) produces the expected plot:
(Though note the y-axis units would be m/s if this wasn't an issue.)
I'd be fine with using either of these workarounds but it means I can't combine a quiver plot with other types of plots that do work fine with mixed units (plot, contour, etc).
Versions:
unyt: 3.0.3 matplotlib: 3.10.0 numpy: 2.2.0
Solution/Comments
Like I said at the top, I don't really have a solution due to how quiver is implemented, but I think at minimum adding something to the matplotlib_support documentation would be helpful.
Let me know if you need any more details.
The text was updated successfully, but these errors were encountered:
mtryan83
changed the title
matplotlib_support: Quiver plot doesn't work
matplotlib_support: Quiver plot doesn't work with mixed units
Mar 3, 2025
Thanks for reporting this.
At first glance, I would think of this as a bug in matplotlib: calling np.column_stack((X, Y)) doesn't seem to make sense in this context and I believe unyt is correct in raising an exception when X and Y do not have the same unit. Unfortunately I don't see how unyt's error message could be improved here, but I'm opened to suggestions.
Additionally, I would also assume that it could easily be fixed in matplotlib by stripping out units in a generic way, doing something like
as you mentionned, other issues might pop up later down the line, but it seems worth to give it a shot first. I don't see any actionable for unyt at this point.
It seems like matplotlib's quiver plot isn't supported in the
matplotlib_support
context manager when x and y have mixed units. I'm not really sure this a unyt problem (because of howquiver
works), so feel free to close this, but I think it should be at least mentioned in the documentation.Example
produces the error:
Note the primary problem appears to be the xy column stacking (the
self.xy = np.column_stack((X, Y))
, but additional problems may show up if for examplex
andxdot
have different units. This isn't surprising but a better error message may be helpful.Workarounds
Changing the last lines to
or using (artificially) consistent units (e.g. changing
y
,u
,v
units to meters) produces the expected plot:(Though note the y-axis units would be m/s if this wasn't an issue.)
I'd be fine with using either of these workarounds but it means I can't combine a quiver plot with other types of plots that do work fine with mixed units (
plot
,contour
, etc).Versions:
unyt: 3.0.3
matplotlib: 3.10.0
numpy: 2.2.0
Solution/Comments
Like I said at the top, I don't really have a solution due to how
quiver
is implemented, but I think at minimum adding something to thematplotlib_support
documentation would be helpful.Let me know if you need any more details.
The text was updated successfully, but these errors were encountered: