繁体   English   中英

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

[英]Visualizing the angle of direction of a parameter on a heatmap

我正在尝试使用下面的代码可视化我的参数之一的减少角度,但是它给出了以下错误。 我将不胜感激任何反馈。

我得到的错误:

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)

            

文档明确告诉我们,构造函数的第一个参数必须是 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM