Visualizing the angle of direction of a parameter on a heatmap

Question:

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:Usersbk76Angle_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:Usersbk76.condaenvstensor-gpulibsite- 
 packagesmatplotlibquiver.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)

            
Asked By: jam_pyt

||

Answers:

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')
Answered By: Andrey Sobolev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.