Matplotlib 等高线
Matplotlib contour
我对 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)
这就是我得到的情节:
问题是比例不一样。通常它应该 "flow in each other"。我并不是说情节不流畅,我知道我可以通过增加 plt.contourf 函数中的 10 来改变这一点。
出现这个问题是因为我"divided"四个区的剧情吗?
所以我解决了这个问题。我与 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')
这是完整的工作代码。我希望这会对某人有所帮助。
我对 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)
这就是我得到的情节:
问题是比例不一样。通常它应该 "flow in each other"。我并不是说情节不流畅,我知道我可以通过增加 plt.contourf 函数中的 10 来改变这一点。
出现这个问题是因为我"divided"四个区的剧情吗?
所以我解决了这个问题。我与 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')
这是完整的工作代码。我希望这会对某人有所帮助。