python 可视化热图上参数的方向角

lnlaulya  于 2023-02-02  发布在  Python
关注(0)|答案(1)|浏览(104)

我试图用下面的代码来可视化我的一个参数的减少Angular ,但是它给出了下面的错误。我将感谢任何反馈。
我得到的错误:

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'

我尝试的代码:

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()

我还尝试用以下代码替换Q代码行,但仍然得到相同的错误:

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)
clj7thdc

clj7thdc1#

文档明确告诉我们,构造函数的第一个参数必须是Axes示例:

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')

相关问题