[英]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.