使用matplotlib pcolor进行插值 [英] interpolation with matplotlib pcolor

查看:333
本文介绍了使用matplotlib pcolor进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组,第一个是(30,365),包含一年中30个深度的值,第二个数组是(30,1),并包含与深度对应的实际深度(以米为单位)第一个数组的深度.我想绘制第一个数组,以便根据第二个数组缩放深度,但我也希望对数据进行插值(前几个深度相对靠近,而较低的深度相距较远,从而给pcolor图像带来了块状外观)

I have two numpy arrays, the first one is (30, 365) and contains values for for 30 depths throughout the year, the second array is (30, 1) and contains the actual depth (in meters) corresponding to the depths in the first array. I want to plot the first array so the depths are scaled according to the second array but I also want the data to be interpolated (the first few depths are relatively close together while lower depths are far apart, providing a blocky look to the pcolor image.)

这就是我在做什么:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 365, 1)
X, Y = np.meshgrid(x, depth)    #depth is the (30, 1) array

plt.pcolor(X, -Y, data)         #data is the (30, 365) array

这会导致块状外观,关于如何获得更平滑外观的图形的任何想法?

which results in the blocky look, any ideas on how I could get a smoother looking graph?

推荐答案

您在常规网格上的深度(即恒定间距)吗?如果是这样,可以使用imshow并使用extent关键字和aspect='auto'设置范围.否则,您有两种选择.

Are your depths on a regular grid (ie, constant spacing)? If so, you can use imshow and set the range with the extent keyword and aspect='auto'. Otherwise, you have two choices.

您可以改为使用pcolormesh,然后使用shading='gouraud'.这将有助于进行清晰的色彩量化,但不如内插好.

You can use pcolormesh instead and use shading='gouraud'. This will help with the sharp colour quantisation, but not as good as interpolation.

第二个选择是将数据插值到新的常规深度网格,因此可以使用imshow和其他插值选项.例如,要仅沿深度方向进行插值,可以使用scipy的interpolate.interp1d:

The second choice is to interpolate data to a new regular depth grid, so you can use imshow and the different interpolation options. For example, to interpolate only along the depth direction you can use scipy's interpolate.interp1d:

from scipy.interpolate import interp1d

fint = interp1d(depth, data.T, kind='cubic')
newdata = fint(newdepth).T

之所以添加了.T,是因为插值必须在最后一个索引上,而深度是数据的第一个索引.如果愿意,可以将kind替换为'linear'.

The .T were added because interpolation has to be on the last index, and depth is the first index of your data. You can replace kind by 'linear' if you prefer.

这篇关于使用matplotlib pcolor进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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