我有一些2D数据,正在使用pcolormesh显示,我希望在其上方显示几个等高线。我使用以下方法创建分格数据:
import numpy as np
import matplotlib.pyplot as plt
def bin(x, y, nbins, weights=None):
hist, X, Y = np.histogram2d(x, y, bins=nbins, weights=weights)
x_grid, y_grid = np.meshgrid(X,Y)
return hist, x_grid, y_grid
data = ... # read from binary file
h,x_grid,y_grid = bin(data.x,data.y,512)
# do some calculations with h
h = masked_log(h) # "safe" log that replaces <0 elements by 0 in output
pcm = plt.pcolormesh(x_grid,y_grid,h,cmap='jet')
# Just pretend that the data are lying on the center of the grid
# points, rather than on the edges
cont = plt.contour(x_grid[0:-1,0:-1],y_grid[0:-1,0:-1],h,4,colors='k',origin='lower')
当我只绘制
pcolormesh
的输出时,一切看起来很好。但是添加等高线会使得画面变得非常混乱。
我已经阅读了等高线演示、API 示例、pcolormesh levels示例和这篇相关的SO文章(我的数据已经网格化,因此该解决方案没有帮助)。但迄今为止,我尝试的所有方法都没有在
pcolormesh
数据上创建出4个简单的等高线。
levels = np.linspace(-4, 6, 4)
用于plt.contour(... , levels = levels)
可能也有所帮助......我建议上传你的数据以提供一个最小示例,这样人们才能提供帮助。 - Ed Smith