在地形上方的python/matplotlib中轮廓化非均匀2D数据 [英] Contouring non-uniform 2d data in python/matplotlib above terrain

查看:90
本文介绍了在地形上方的python/matplotlib中轮廓化非均匀2D数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matplotlib中为某些数据绘制轮廓时遇到了麻烦.我正在尝试绘制从3d温度场切下来的温度的垂直横截面.

I am having trouble contouring some data in matplotlib. I am trying to plot a vertical cross-section of temperature that I sliced from a 3d field of temperature.

我的温度阵列(T)的尺寸为50 * 300,其中300是均匀间隔的水平高度的数量.但是,垂直高度的数量为50,它们是:a)间距不均匀; b)每个垂直列的起始级别都不同.就像在其中,总是有50个垂直水位,但有时它们的跨度为100-15000 m,有时跨度为300-20000 m(由于地形差异).

My temperature array (T) is of size 50*300 where 300 is the number of horizontal levels which are evenly spaced. However, 50 is the number of vertical levels that are: a) non-uniformly spaced; and b) have a different starting level for each vertical column. As in there are always 50 vertical levels, but sometimes they span from 100 - 15000 m, and sometimes from 300 - 20000 m (due to terrain differences).

我还具有一个2d高度数组(Z;与T形状相同),一个1d水平位置数组(LAT)和一个1d地形高度数组(TER).

I also have a 2d array of height (Z; same shape as T), a 1d array of horizontal location (LAT), and a 1d array of terrain height (TER).

我正在尝试获得与此处,您可以在其中看到地形被涂黑,并且周围的数据具有轮廓.

I am trying to get a similar plot to one like here in which you can see the terrain blacked out and the data is contoured around it.

我作图的第一步是创建一个水平距离和高度的网格,然后用这些参数创建轮廓温度.但是numpy.meshgrid需要1d输入,而我的身高是2d变量.这样做只是从第一列开始向上轮廓:

My first attempt to plot this was to create a meshgrid of horizontal distance and height, and then contourf temperature with those arguments as well. However numpy.meshgrid requires 1d inputs, and my height is a 2d variable. Doing something like this only begins contouring upwards from the first column:

ax1 = plt.gca()
z1, x1 = np.meshgrid(LAT, Z[:,0])
plt.contourf(z1, x1, T)
ax1.fill_between(z1[0,:], 0, TER, facecolor='black')

产生.如果我在网状网格中使用Z [:,-1],它会在地下为左侧的列绘制轮廓,这显然是我所不希望的.我真正想要的是在Meshgrid中为Z使用一些2d数组,但是我不确定该怎么做.

Which produces this. If I use Z[:,-1] in the meshgrid, it contours underground for columns to the left, which obviously I don't want. What I really would like is to use some 2d array for Z in the meshgrid but I'm not sure how to go about that.

我也研究了griddata函数,但这也需要一维输入.有人对如何解决这个问题有任何想法吗?任何帮助表示赞赏!

I've also looked into the griddata function but that requires 1D inputs as well. Anyone have any ideas on how to approach this? Any help is appreciated!

推荐答案

根据我的理解,您的数据是结构化的.然后,您可以直接使用matplotlib中的contourfcontour选项.您提供的代码具有正确的想法,但您应该使用

For what I understand your data is structured. Then you can directly use the contourf or contour option in matplotlib. The code you present have the right idea but you should use

x1, z1 = np.meshgrid(LAT, Z[:,0])
plt.contourf(x1, Z, T)

用于轮廓.我在下面有一个例子

for the contours. I have an example below

import numpy as np
import matplotlib.pyplot as plt


L, H = np.pi*np.mgrid[-1:1:100j, -1:1:100j]
T = np.cos(L)*np.cos(2*H)
H = np.cos(L) + H

plt.contourf(L, H, T, cmap="hot")
plt.show()

看起来网格是用原始边界框生成的,但是该图是用已变换的高度而不是初始高度绘制的.另外,您可以将 tricontour 用于非结构化数据(或一般而言),但是那么您将需要生成三角剖分(在您的情况下很简单).

Look that the grid is generated with the original bounding box, but the plot is made with the height that has been transformed and not the initial one. Also, you can use tricontour for nonstructured data (or in general), but then you will need to generate the triangulation (that in your case is straightforward).

这篇关于在地形上方的python/matplotlib中轮廓化非均匀2D数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆