用plt在热图上绘制渐变箭头 [英] Plot gradient arrows over heatmap with plt

查看:203
本文介绍了用plt在热图上绘制渐变箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制箭头以可视化热图上的渐变.这是我到目前为止的代码:

I am trying to plot arrows to visualize the gradient over a heatmap. This is the code I have until now:

import matplotlib.pyplot as plt
import numpy as np
function_to_plot = lambda x, y: x + y ** 2
horizontal_min, horizontal_max, horizontal_stepsize = 0, 3, 0.3
vertical_min, vertical_max, vertical_stepsize = 0, 3, 0.6

xv, yv = np.meshgrid(np.arange(horizontal_min, horizontal_max, horizontal_stepsize), 
                     np.arange(vertical_min, vertical_max, vertical_stepsize))

result_matrix = function_to_plot(xv, yv)
xd, yd = np.gradient(result_matrix)

def func_to_vectorize(x, y, dx, dy, scaling=1):
    plt.arrow(x + horizontal_stepsize/2, y + vertical_stepsize/2, dx*scaling, dy*scaling, fc="k", ec="k", head_width=0.1, head_length=0.1)

vectorized_arrow_drawing = np.vectorize(func_to_vectorize)

plt.imshow(result_matrix, extent=[horizontal_min, horizontal_max, vertical_min, vertical_max])
vectorized_arrow_drawing(xv, yv, xd, yd, 1)
plt.colorbar()
plt.show()

这是结果图:

我期望箭头指向具有最大值的矩形,但事实并非如此.我想念什么?

I was expecting the arrows to point toward the rectangles with greatest values, but they aren't. What am I missing?

推荐答案

  1. np.gradient()似乎在x值之前返回 y值
  2. 颜色也似乎不正确,因为图形上下文的y值被颠倒了.因此,我在绘图过程中使用了np.flip(result_matrix,0)
  3. 最后,我注意到当stepsize不能均匀划分区域时,绘制箭头时会出现小故障,此外,网格未与框的中心对齐.我已经在以下代码中修复了这两个问题:
  1. It looks as though np.gradient() returns y-values before x-values
  2. The colors also appeared to be incorrect because the graphing context y-values were reversed. Thus I used np.flip(result_matrix,0) during plotting
  3. Finally, I noticed that there was a glitch when plotting the arrows when the stepsize did not divide the region evenly, in addition the mesh was not aligned to the center of the boxes. I have fixed both of these in the following code:

这是我用来生成图形的代码:

Here is the code which I used to generate the graph:

import matplotlib.pyplot as plt
import numpy as np
import math
function_to_plot = lambda x, y: x**2 + y**2
horizontal_min, horizontal_max, horizontal_stepsize = -2, 3, 0.3
vertical_min, vertical_max, vertical_stepsize = -1, 4, 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 = function_to_plot(xv, yv)
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()

这篇关于用plt在热图上绘制渐变箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆