【问题标题】:How to plot gradient vector on contour plot in python如何在python中的等高线图上绘制梯度向量
【发布时间】:2020-09-11 08:16:34
【问题描述】:

我有两个变量 W1、W2 和输出 z = F(W1,W2) 的损失函数。

现在我绘制这个损失函数的等高线图。现在说,我已经计算了两个点的梯度向量,因此我现在有两个梯度向量。我想在我的等高线图上绘制这些梯度向量,但我不知道如何处理。任何帮助表示赞赏

enter code here
%matplotlib inline
import matplotlib.pyplot as plt 
import numpy as np 

feature_x = np.arange(-50, 50, 2) 
feature_y = np.arange(-50, 50, 3) 

# Creating 2-D grid of features 
[X, Y] = np.meshgrid(feature_x, feature_y) 

fig, ax = plt.subplots(1, 1) 

z = 0.5*np.array((Y-X)*(Y-X) + 0.5*(1-X)*(1-X))

# plots contour lines 
ax.contour(X, Y, z, 10, cmap = 'jet') 
ax.grid(True)
ax.axis('scaled')
#ax.clabel(cp, inline=1, fontsize=10)  
ax.set_title('Contour Plot') 
ax.set_xlabel('feature_x') 
ax.set_ylabel('feature_y') 

plt.show() 

【问题讨论】:

    标签: python matplotlib gradient contour


    【解决方案1】:

    您可以使用FancyArrowPatch 在几个选定的位置绘制渐变。

    from matplotlib.patches import FancyArrowPatch
    x1 = -20     # position of the gradient
    y1 = 10
    dz1_dx = 10  # value of the gradient at that position
    dz1_dy = -5
    arrow = FancyArrowPatch((x1, y1), (x1+dz1_dx, y1+dz1_dy),    
                            arrowstyle='simple', color='k', mutation_scale=10)
    ax.add_patch(arrow)
    

    否则,如果您想绘制整个矢量场 quiver 可能是一个选项:

    feature_x = np.arange(-50, 50, 2)
    feature_y = np.arange(-50, 50, 2)
    
    x, y = np.meshgrid(feature_x, feature_y)
    z = 0.5*(y-x)**2 + 0.5*(1-x)**2
    u = 2*x - y - 1
    v = y - x
    
    # Normalize all gradients to focus on the direction not the magnitude
    norm = np.linalg.norm(np.array((u, v)), axis=0)
    u = u / norm
    v = v / norm
    
    fig, ax = plt.subplots(1, 1)
    ax.set_aspect(1)
    ax.plot(feature_x, feature_y, c='k')
    ax.quiver(x, y, u, v, units='xy', scale=0.5, color='gray')
    ax.contour(x, y, z, 10, cmap='jet', lw=2)
    
    arrow = FancyArrowPatch((35, 35), (35+34*0.2, 35+0), arrowstyle='simple',
                            color='r', mutation_scale=10)  
    ax.add_patch(arrow)  # NOTE: this gradient is scaled to make it better visible
    

    我在这个图中添加了line y = x,并标记了这条线与等高线相交的点。在这里你可以清楚地看到 那:

    渐变与水平表面正交

    因此,对于您的观点(80, 80),渐变(79, 0) 是正确的,即使等值线的一般形状可能表明应该在 y 方向上存在一部分。 但是如果你沿着 y=x 线看,你会发现那里的渐变总是只在 x 方向上。

    【讨论】:

    • 我更新了我的答案并包含了第二个示例,我在其中绘制了整个向量场,希望这能让事情更清楚。但是对于(80, 80) 的位置/坐标是可以的,渐变是(79, 0)
    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多