Python 具有非均匀矩阵网格的Matplotlib等高线图

Python 具有非均匀矩阵网格的Matplotlib等高线图,python,matplotlib,Python,Matplotlib,我想画一个函数f(x,y)与x和x-y的等高线图。y轴网中的间距与x轴网不同,因此x-y是二维的,而x是一维的 我不知道如何设置网格。函数“tricontourf”可以处理非均匀网格,但只有在两个轴都是一维的情况下才能处理contour可以处理矩阵,但只处理f(x,y),而我需要一个轴作为矩阵 伪代码如下所示: import matplotlib.pyplot as plt def twoDfunction(x,y): return x + y # my function is more

我想画一个函数f(x,y)与x和x-y的等高线图。y轴网中的间距与x轴网不同,因此x-y是二维的,而x是一维的

我不知道如何设置网格。函数“
tricontourf
”可以处理非均匀网格,但只有在两个轴都是一维的情况下才能处理
contour
可以处理矩阵,但只处理f(x,y),而我需要一个轴作为矩阵

伪代码如下所示:

import matplotlib.pyplot as plt

def twoDfunction(x,y):
   return x + y # my function is more complicated than this

xaxis = np.linspace(0,10,100)
yaxis = np.linspace(0,10,22)
xminusyaxis = np.subtract(xaxis,yaxis)
functionsurfacevalues = twoDfunction(xaxis,yaxis)

fig =plt.figure(figsize=(10,10),dpi=300,facecolor='w')
ax1 = plt.subplot(111)
ax1.tricontourf(xaxis, xminusyaxis, functionsurfacevalues)


我想让伪代码绘制
函数surfacevalue
x
xminusy

您需要做的是使用
np.meshgrid()创建网格
然后绘制一个
等高线
等高线
绘图。
np.meshgrid
将根据您给定的内容生成不规则网格。您不需要曲面打印,因为您的数据实际上不是曲面

你面临的主要问题是,因为你的x轴和y轴的长度不同,你不能减去它们。否则,解决方案很简单,您可以遵循以下代码

import matplotlib.pyplot as plt

def twoDfunction(x,y):
   return (x + y) # my function is more complicated than this

xaxis = np.linspace(0,10,100)
yaxis = np.linspace(0,5,100)

xminusyaxis = np.subtract(xaxis,yaxis)

xx,yy = np.meshgrid(xaxis,xminusyaxis)

fig =plt.figure(figsize=(10,10),dpi=300,facecolor='w')
ax1 = plt.subplot(111)
ax1.contourf(xx, yy, twoDfunction(xx,yy))


plt.show()

当xminusy是矩阵而不是向量时,如何使用Contourf?到目前为止,
surface\u plot
是可行的,但它并不优雅。我不知道如何从减去2个向量得到矩阵?我不清楚。对于给定的y,xminusy网格将移动y。因此,如果有ny点和mx点,xminusy网格的大小将为nx M。