将pcolormesh和轮廓放到同一网格上? [英] Put pcolormesh and contour onto same grid?

查看:51
本文介绍了将pcolormesh和轮廓放到同一网格上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 contour pcolormesh 来显示带有轴标签的2D数据.正如 matplotlib 用户列表中所指出的,这些函数遵循不同的约定:pcolormesh 期望 x 和 y 值指定单个像素的,而 轮廓需要像素的中心.

I'm trying to display 2D data with axis labels using both contour and pcolormesh. As has been noted on the matplotlib user list, these functions obey different conventions: pcolormesh expects the x and y values to specify the corners of the individual pixels, while contour expects the centers of the pixels.

使这些行为始终如一的最佳方法是什么?

What is the best way to make these behave consistently?

我考虑过的一个选项是制作一个从中心到边缘"的功能,假设数据间隔均匀:

One option I've considered is to make a "centers-to-edges" function, assuming evenly spaced data:

def centers_to_edges(arr):
    dx = arr[1]-arr[0]
    newarr = np.linspace(arr.min()-dx/2,arr.max()+dx/2,arr.size+1)
    return newarr

另一种选择是将 imshowextent 关键字集一起使用.
第一种方法不能很好地处理 2D 轴(例如,由 meshgridindices 创建),第二种方法完全丢弃轴号

Another option is to use imshow with the extent keyword set.
The first approach doesn't play nicely with 2D axes (e.g., as created by meshgrid or indices) and the second discards the axis numbers entirely

推荐答案

您的数据是常规网格?如果没有,您可以使用 griddata() 来获取它.我认为如果你的数据太大,子采样或正则化总是可能的.如果数据太大,则与之相比,您的输出图像可能总是很小,您可以利用它.如果将 imshow() 与extent"和interpolation='nearest'"一起使用,您将看到数据以单元格为中心,并且范围提供单元格的下边缘(角).另一方面,contour 假设数据以单元格为中心,X,Y 必须是单元格的中心.所以,你需要关心轮廓的输入域.简单的例子是:

Your data is a regular mesh? If it doesn't, you can use griddata() to obtain it. I think that if your data is too big, a sub-sampling or regularization always is possible. If the data is too big, maybe your output image always will be small compared with it and you can exploit this. If you use imshow() with "extent" and "interpolation='nearest'", you will see that the data is cell-centered, and extent provided the lower edges of cells (corners). On the other hand, contour assumes that the data is cell-centered, and X,Y must be the center of cells. So, you need to be care about the input domain for contour. The trivial example is:

x = np.arange(-10,10,1)
X,Y = np.meshgrid(x,x)
P = X**2+Y**2
imshow(P,extent=[-10,10,-10,10],interpolation='nearest',origin='lower')
contour(X+0.5,Y+0.5,P,20,colors='k')

我的测试告诉我pcolormesh()是一个非常慢的例程,我总是尝试避免使用它.griddata和imshow()始终是我的不错选择.

My tests told me that pcolormesh() is a very slow routine, and I always try to avoid it. griddata and imshow() always is a good choose for me.

这篇关于将pcolormesh和轮廓放到同一网格上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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