简体   繁体   English

来自xyz数据的Matplotlib等高线图

[英]Matplotlib contour plot from xyz data with scale

I'm trying to draw a 2D contour plot in matplotlib using xyz data. 我正在尝试使用xyz数据在matplotlib中绘制2D等高线图。 I have two sets of Z data I want to plot (z_a and z_b). 我要绘制两组Z数据(z_a和z_b)。 I have been able to plot the data and draw a contour plot however I have been unable to put a scale on either graph (the scale should be for the z data). 我已经能够绘制数据并绘制等高线图,但是我无法在两个图形上都设置比例尺(比例尺应针对z数据)。

When I use the following code I get an error saying 'TypeError: Input z must be a 2D array.' 当我使用以下代码时,出现错误消息“ TypeError:输入z必须是2D数组。” I thought my z data was in an array so I'm not quite sure why I get this error, could anyone shed some light on this problem? 我以为我的z数据在一个数组中,所以我不太确定为什么会收到此错误,有人可以阐明这个问题吗? Thanks! 谢谢!

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()

You can use griddata something like this: 您可以使用如下所示的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM