Reputation: 103
I have some problems with the contour plot in matplotlib. I divided my plot in 4 areas,
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)
The problem is, that the proportians are not the same. Normally it should "flow in each other". And I'm not saying that the plot isn't smooth or so, I know i can change this by increasing the 10 in the plt.contourf functions.
Does this problem occur because I "divided" the plot in four areas?
Upvotes: 1
Views: 579
Reputation: 103
So I solved the problem. I has to do with the nature of contourf. If you have a plot with a 3x2 grid then in the first row you have your values for your
y1->x1,x2,x3
second row would be
y2->x1,x2,x3
and third row
y3->x1,x2,x3
That means that your matrix with the values must have this structure.
I didnt know this and because I had to deal with 4 times 100x100 grid areas there was no problem with the matrix (but values). After I've changed the matrix shape I could figure out what the problem was. The FIGURE and the code:
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')
This is the whole working code. I hope this will help someone out.
Upvotes: 1