I am trying to visualize the angle of reduction for one of my parameter using the code below, however it is giving the following error. I would appreciate any feedback.
The error that I am getting:
Traceback (most recent call last):
File "C:\Users\bk76\Angle_of_parameter_reduction.py", line 185, in
<module>
Q = Quiver(np.arange(0, parameter_data.shape[1], 5), np.arange(0,
parameter_data.shape[0], 5), dx[::5, ::5], dy[::5, ::5], angles='xy',
scale_units='xy', scale=2, color='blue')
File "C:\Users\bk76\.conda\envs\tensor-gpu\lib\site-
packages\matplotlib\quiver.py", line 486, in __init__
self.transform = kw.pop('transform', ax.transData)
AttributeError: 'numpy.ndarray' object has no attribute 'transData'
The code that I tried:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from matplotlib.quiver import Quiver
# Define parameter data as a 2D array
np.seed(1)
parameter_data = np.random.rand(150, 150)
# # Compute angle of parameter direction
# dy, dx = np.gradient(parameter_data)
# Compute angle of parameter direction
dy, dx = np.gradient(parameter_data)
angle = np.arctan2(dy, dx)
# Create figure and axes
fig, ax = plt.subplots()
# Plot parameter data as heatmap
im = ax.imshow(parameter, cmap='hot', norm=LogNorm())
# Create vector field of parameter direction
Q = Quiver(np.arange(0, parameter_data.shape[1], 5), np.arange(0, parameter_data.shape[0], 5), dx[::5, ::5], dy[::5, ::5], angles='xy', scale_units='xy', scale=2, color='blue')
ax.add_collection(Q)
# Add colorbar and labels
cbar = fig.colorbar(im, ax=ax)
cbar.set_label('Parameter')
ax.set_xlabel('X')
ax.set_ylabel('Y')
# Show plot
plt.show()
I also tried replaing the Q code line with the following, and still got the same error:
Q = Quiver(np.arange(0, parameter_data.shape[1], 5), np.arange(0, parameter_data.shape[0], 5),
dx[::5, ::5], dy[::5, ::5], angles=angle[::5, ::5].ravel(), scale_units='xy', scale=2,
color='blue', pivot='mid', transform=ax.transData)
CodePudding user response:
The docs explicitly tell us, that the first argument to the constructor has to be the Axes instance:
Q = Quiver(ax, np.arange(0, parameter_data.shape[1], 5), np.arange(0, parameter_data.shape[0], 5), dx[::5, ::5], dy[::5, ::5], angles='xy', scale_units='xy', scale=2, color='blue')