来自xyz数据的Matplotlib等高线图具有比例

时间:2016-04-12 17:06:04

标签: python matplotlib

我尝试使用xyz数据在matplotlib中绘制2D等高线图。我有两组想要绘制的Z数据(z_a和z_b)。我已经能够绘制数据并绘制轮廓图但是我无法在任一图表上放置比例(比例应该是z数据)。

当我使用下面的代码时,我收到错误提示' TypeError:输入z必须是2D数组。'我以为我的z数据是在一个数组中,所以我不太清楚为什么会出现这个错误,有人能解释一下这个问题吗?谢谢!

import numpy as np
import matplotlib.pyplot as plt

x = [68,77,95,111,120,148,148,148,175,185,200,218,228]
y = [135,190,87,149,226,68,112,187,225,149,87,190,135]


z_a = [22.87,22.87,23.75,22.81,22.94,22.94,22.94,22.81,22.87,23.06,23.06,23.0,23.12]
z_b = [24.06,24.06,24.94,24.0,24.06,24.12,24.19,24.06,24.12,24.25,24.25,24.25,30]

f, ax = plt.subplots(1,2, sharex=True, sharey=True)

ax[0].tricontourf(x,y,z_a, 100) 
ax[0].plot(x,y, 'ko ')

ax[1].tricontourf(x,y,z_b, 100) 
ax[1].plot(x,y, 'ko ')

plt.contourf(x, y, z_a, 8)
contour_labels = plt.contour(x, y, z_a, 8, colors='black', linewidth=.5)

plt.show()

1 个答案:

答案 0 :(得分:0)

你可以使用这样的griddata:

from scipy.interpolate import griddata

x = [68,77,95,111,120,148,148,148,175,185,200,218,228]
y = [135,190,87,149,226,68,112,187,225,149,87,190,135]

z_a = [22.87,22.87,23.75,22.81,22.94,22.94,22.94,22.81,22.87,23.06,23.06,23.0,23.12]
z_b = [24.06,24.06,24.94,24.0,24.06,24.12,24.19,24.06,24.12,24.25,24.25,24.25,30]

f, ax = plt.subplots(1,2, sharex=True, sharey=True)

ax[0].tricontourf(x,y,z_a, 100) 
ax[0].plot(x,y, 'ko ')

ax[1].tricontourf(x,y,z_b, 100) 
ax[1].plot(x,y, 'ko ')

xi = np.linspace(np.min(x), np.max(x), 100)
yi = np.linspace(np.min(y), np.max(y), 100)
zia = griddata((x,y),z_a,(xi[None,:],yi[:,None]),method='linear')
zib = griddata((x,y),z_b,(xi[None,:],yi[:,None]),method='linear')

ax[0].contourf(xi, yi, zia, 8)
contour_labels = ax[0].contour(xi, yi, zia, 8, colors='black', linewidth=.5)
ax[0].clabel(contour_labels,inline=1,inline_spacing=0,fontsize=10,fmt='%1.0f',colors='k')

ax[1].contourf(xi, yi, zib, 8)
contour_labels = ax[1].contour(xi, yi, zib, 8, colors='black', linewidth=.5)
ax[1].clabel(contour_labels,inline=1,inline_spacing=0,fontsize=10,fmt='%1.0f',colors='k')

plt.show()