【问题标题】:Adding a crosshair or marker to a matplotlib contour plot向 matplotlib 等高线图添加十字准线或标记
【发布时间】:2014-11-04 07:55:46
【问题描述】:

我正在使用 matplotlib 将 NumPy 数组绘制为等高线图:

import numpy as np
import matplotlib.pyplot as plt

plt.contour(array, linewidths = 1, colors = 'k')
plt.contourf(array, cmap = plt.cm.jet)
plt.colorbar()
plt.show()

我想添加一个“十字准线”或其他标记来表示数组中的最大值,该最大值由以下公式给出:

maxi = np.max(array)

我该怎么做呢?

【问题讨论】:

  • 您可以使用您选择的标记在该点进行绘图,或者您可以使用axvlineaxhlin 创建到轴边缘的交叉线。
  • @tcaswell 我不熟悉语法(由于某种原因找不到它)。你知道或有链接吗?

标签: python python-2.7 matplotlib


【解决方案1】:

如果你知道位置,你可以简单地画十字。

[row, col] = numpy.where(array==np.max(array))
plt.plot(col, row, 'b+')

更改标记大小check this

【讨论】:

  • 太好了,现在如何将垂直和水平线添加到这一点
【解决方案2】:

我添加了代码以使用 xs 和 ys 垂直和水平线的 6 个增量来显示 b+ 标记的十字准线

b1=1.8027335249990852
xs=[4]*6
ys=np.linspace(0,int(4*b1),6)
ys2=[int(4*b1)]*6
xs2=np.linspace(0,4,6)

plt.plot(xs, ys,'k-', linestyle = ":", lw=1)
plt.plot(xs2, ys2,'k-', linestyle = ":", lw=1)
plt.plot(4, 4*b1, 'b+')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    相关资源
    最近更新 更多