Matplotlib轮廓

时间:2016-09-10 17:10:30

标签: matplotlib contourf

我在matplotlib中的轮廓图有一些问题。我把我的情节划分为4个区域,

a1=zeros((100,100))
a2=zeros((100,100))
a3=zeros((100,100))
a4=zeros((100,100))


x=np.linspace(x1,x2,100) #x1,x2,y1,y2 and so on are boundaries I didnt include here
y=np.linspace(y1,y2,100)

xneu=np.linspace(x2,x3,100)
yneu=np.linspace(y1,y2,100)

yo=np.linspace(y1,y3,100)

#Four areas X,Y X1,Y1 X2,Y2 X3,Y3

X, Y=np.meshgrid(x, y)
X1, Y1=np.meshgrid(xneu, y)

X2,Y2=np.meshgrid(x,yo)
X3,Y3=np.meshgrid(xneu,yo)

#filling my arrays with wanted values , f's are functions I haven't included here
for i in arange(0,len(y)):
    werte[i]=f(y[i])

for j in arange(0, len(xneu)):
    for i in arange(0, len(yneu)):
        werte2[i][j]=f1(xneu[i],yneu[j]) + f3(xneu[i],yneu[j]) + f5(xneu[i],yneu[j]) + f7(xneu[i],yneu[j]) + f9(xneu[i],yneu[j]) 

for i in arange(0,len(yo)):
    werte3[i]=f(y[i])

for j in arange(0, len(xneu)):
    for i in arange(0, len(yo)):
        werte4[i][j]=f1(xneu[i],yo[j]) + f3(xneu[i],yo[j]) + f5(xneu[i],yo[j]) + f7(xneu[i],yo[j]) + f9(xneu[i],yo[j])     

cs = plt.contourf(X, Y, werte, 10)    
ds = plt.contourf(X1, Y1, werte2, 10)
es = plt.contourf(X2, Y2, werte3, 10)    
fs = plt.contourf(X3, Y3, werte4, 10)

这是我得到的情节: Plot

问题是,比例是不一样的。通常它应该“相互流动”。而且我不是说情节不顺畅,我知道我可以通过增加plt.contourf函数中的10来改变它。

是否会出现此问题,因为我将图块“划分”为四个区域?

1 个答案:

答案 0 :(得分:1)

所以我解决了这个问题。我与contourf的本质有关。如果你有一个3x2网格的图,那么在第一行你有你的

y1-> x1,x2,x3

第二行将是

的Y2> X1,X2,X3

和第三行

Y3-> X1,X2,X3

这意味着带有值的矩阵必须具有此结构。

我不知道这一点,因为我必须处理4次100x100网格区域,矩阵没有问题(但值)。在我改变矩阵形状后,我可以弄清楚问题是什么。 FIGURE和代码:

from numpy import pi,arange,cos,sinh, zeros
import numpy as np
import matplotlib.pyplot as plt


w=100
b=40
V0=10.0
a=200.0

x1=0
x2=w/2.0
x3=a/2

y1=0
y2=-b/2.0

y3=b/2.0


A1=(8*V0)/((1**2)*(pi**2)*sinh(((1*pi)/(2*b))*(a-w)))
A2=(8*V0)/((3**2)*(pi**2)*sinh(((3*pi)/(2*b))*(a-w)))
A3=(8*V0)/((5**2)*(pi**2)*sinh(((5*pi)/(2*b))*(a-w)))
A4=(8*V0)/((7**2)*(pi**2)*sinh(((7*pi)/(2*b))*(a-w)))
A5=(8*V0)/((9**2)*(pi**2)*sinh(((9*pi)/(2*b))*(a-w)))


werte=zeros((20, 50))
werte2=zeros((20, 50))
werte3=zeros((20, 50))
werte4=zeros((20, 50))

def f(y):
    global V0, b
    return (2*V0/b)*y + V0


def f1(x,y):
    global A1,b,a, w
    return A1*cos(pi*y/b)*sinh((pi/b)*(a/2.0-x))


def f3(x,y):
    global A2, b, a, w
    return A2*cos(3*pi*y/b)*sinh((3*pi/b)*(a/2.0-x))


def f5(x,y):
    global A3, b, a, w
    return A3*cos(5*pi*y/b)*sinh((5*pi/b)*(a/2.0-x))

def f7(x,y):
    global A4, b, a, w
    return A4*cos(7*pi*y/b)*sinh((7*pi/b)*(a/2.0-x))

def f9(x,y):
    global A5, b, a, w
    return A5*cos(9*pi*y/b)*sinh((9*pi/b)*(a/2.0-x))

x=np.linspace(x1,x2,50)

y=np.linspace(y1,y2,20)

xneu=np.linspace(x2,x3,50)
yo=np.linspace(y1,y3,20)



X, Y = np.meshgrid(x, y)
X1, Y1=np.meshgrid(xneu, y)
X2,Y2=np.meshgrid(x,yo)
X3,Y3=np.meshgrid(xneu,yo)

for i in arange(0,len(y)):
    for j in arange(0, len(x)):        
        werte[i][j]=f(y[i])

for i in arange(0, len(xneu)):
    for j in arange(0, len(y)):
        werte2[j][i]=f1(xneu[i],y[j]) + f3(xneu[i],y[j]) + f5(xneu[i],y[j]) + f7(xneu[i],y[j]) + f9(xneu[i],y[j]) 


for i in arange(0,len(yo)):
    for j in arange(0,len(x)):
        werte3[i][j]=f(y[i])


for i in arange(0, len(xneu)):
    for j in arange(0, len(yo)):
        werte4[j][i]=f1(xneu[i],yo[j]) + f3(xneu[i],yo[j]) + f5(xneu[i],yo[j]) + f7(xneu[i],yo[j]) + f9(xneu[i],yo[j])     


print(werte2)

cs = plt.contourf(X, Y, werte, 10)    
ds = plt.contourf(X1, Y1, werte2, 10)
es = plt.contourf(X2, Y2, werte3, 10)    
fs = plt.contourf(X3, Y3, werte4, 10)

plt.colorbar(cs)
#plt.colorbar(ds)

#plt.clabel(cs,inline=0, fontsize=10, colors='black')
#plt.clabel(ds,inline=0, fontsize=10, colors='black')

这是整个工作代码。我希望这会帮助别人。