【发布时间】:2019-10-02 18:52:10
【问题描述】:
我正在尝试根据我的目的调整此链接中接受的答案代码: Plot gradient arrows over heatmap with plt
我正在做一个项目,该项目需要我以 .csv 文件的形式拍摄热图像,然后从 .csv 文件中获取数据以制作显示方向的箭头(通过 quiverplot streamplot 等)来自图像上最热点(最高像素值)的热流。我认为这可以使用图像的渐变来实现,但我不确定如何实现。
这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
import math
directory = os.chdir(r'user_directory') #Set folder to look in
file = 'data.csv'
data = np.genfromtxt(file, delimiter = ',')
horizontal_min, horizontal_max, horizontal_stepsize = 0, 100, 0.3
vertical_min, vertical_max, vertical_stepsize = 0, 100, 0.5
horizontal_dist = horizontal_max-horizontal_min
vertical_dist = vertical_max-vertical_min
horizontal_stepsize = horizontal_dist / float(math.ceil(horizontal_dist/float(horizontal_stepsize)))
vertical_stepsize = vertical_dist / float(math.ceil(vertical_dist/float(vertical_stepsize)))
xv, yv = np.meshgrid(np.arange(horizontal_min, horizontal_max, horizontal_stepsize),
np.arange(vertical_min, vertical_max, vertical_stepsize))
xv+=horizontal_stepsize/2.0
yv+=vertical_stepsize/2.0
result_matrix = np.asmatrix(data)
yd, xd = np.gradient(result_matrix)
def func_to_vectorize(x, y, dx, dy, scaling=0.01):
plt.arrow(x, y, dx*scaling, dy*scaling), fc="k", ec="k", head_width=0.06,
head_length=0.1)
vectorized_arrow_drawing = np.vectorize(func_to_vectorize)
plt.imshow(np.flip(result_matrix,0), extent=[horizontal_min, horizontal_max, vertical_min,
vertical_max])
vectorized_arrow_drawing(xv, yv, xd, yd, 0.1)
plt.colorbar()
plt.show()
这是我得到的错误:
ValueError: operands could not be broadcast together with shapes (200,335) (200,335) (100,100) (100,100) ()
编辑:回溯错误
ValueError Traceback(最近一次调用最后一次) 中的46 47 plt.imshow(np.flip(result_matrix,0), 范围=[horizontal_min, 水平最大,垂直最小,垂直最大]) ---> 48 vectorized_arrow_drawing(xv, yv, xd, yd, 0.1) 49 plt.colorbar() 50 plt.show() ~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in __call__(self, *args, **kwargs) 1970 vargs.extend([kwargs[_n] for _n in names]) 1971年 -> 1972 返回 self._vectorize_call(func=func, args=vargs) 1973年 1974年定义_get_ufunc_and_otypes(自我,功能,参数): ~\Anaconda3\lib\site-packages\numpy\lib\function_base.py 在 _vectorize_call(self, func, args) 2046 for a in args] 2047 -> 2048 个输出 = ufunc(*输入) 2049 2050 如果 ufunc.nout == 1: ValueError:操作数无法与形状一起广播 (100,168) (100,168) (100,100) (100,100) ()
【问题讨论】:
-
请包括错误的完整回溯,而不仅仅是最后一行,因为这有助于诊断问题
-
对不起,我添加了其余的错误!
标签: python numpy vectorization gradient