【问题标题】:Error while plotting contour in Matplotlib在 Matplotlib 中绘制轮廓时出错
【发布时间】:2016-07-08 07:50:05
【问题描述】:

我有 X coordinatesY coordinatesZcoordinates,每个都存储在带有 n x 1 . (n rows,1 column) 的数组中。Matplotlib 中的等高线图仅在 "*X and Y must both be 2-D with the same shape as Z, or they must both be 1-D such that len(X) is the number of columns in Z and len(Y) is the number of rows in Z.*" 时才允许绘制

我该如何解决这个问题?
相应的 x、y、z 值也应该可以访问。
数据点的混合会导致我的绘图出错。

【问题讨论】:

  • 您希望等高线图在 1D z 坐标下看起来如何(假设 z 是您在 x-y 平面上绘制的)?您确定您不是在寻找散点图或 3d 线图吗?
  • 您可以通过提供代码的最小工作示例来改进此问题,该示例重现了您遇到的错误。

标签: python python-2.7 numpy matplotlib plot


【解决方案1】:

我知道的方法是在由 x 和 y 组成的网格上评估 z:

X, Y = np.meshgrid(x, y)
plt.plot(X, Y, z)
z = <evaluate over X, Y>
plt.contour(X, Y, z)
plt.show()

This 解释得好一点

【讨论】:

  • 您假设 z 被定义为可以在新网格上评估的函数。它可能是一座山的点高度测量列表,所以这种方法行不通。
【解决方案2】:

Contour 需要网格数据。这个 matplotlib 教程完美地解释了它: http://matplotlib.org/examples/pylab_examples/griddata_demo.html。 另见Make contour of scatter

【讨论】:

  • 虽然在这种情况下不太可能发生,但最好始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。 answer guidelines
  • 谢谢 - 请记住这一点 :) 对于这个特定的站点,正如您已经指出的那样,这里不太可能,因为这些站点是 stackoverflow 本身和 matplotlib。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
相关资源
最近更新 更多