Skip to content
Advertisement

Matplotlib with variable upper limit

I need to contourplot one huge expression which is:

I = 1j  # imaginary unit
Psi = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*((1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-1.*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*exp((0.7071067812e-1*I)*cos(x)+1.414213562+.7071067812*I+0.7071067812e-1*cos(x))-(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*exp(-(0.7071067812e-1*I)*cos(x)-.7071067812*I-0.7071067812e-1*cos(x))+(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y+(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y+1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(.9583581328*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-.8189270221*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(.8189270221*I)*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.9583581328*I)*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+.5835053242*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-.4986113867*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(.4986113867*I)*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.5835053242*I)*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))))

which is not so much relevant here.

Usually the task is trivial and can be done this way:

import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh
import matplotlib
from matplotlib import pyplot as plt
delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
x, y = np.meshgrid(X, Y)
# AA = that huge expression 
fig, ax = plt.subplots() 
plt.xlabel(r'x')
plt.ylabel(r'y')
CS = ax.contour(x, y, AA.real)
ax.clabel(CS, inline=True, fontsize=10)
plt.show()

That all in common yields enter image description here

which is okay, but I have an issue I cannot handle.

The problem is that I need to plot it assuming that my y is not a simple array and the upperlimit is variable and must obey

y = 0.1 cos(x). 
So, x=0..6 and y = 0.1 cos(x)

In that case the result has to be

enter image description here

I do not know how to achieve this. So, how to contourplot with variable upper limit? I hope I stated my problem clear.

Any help will be greatly appreciated!

Thank you very much in advance!

Advertisement

Answer

You could use a masked array in order to mask AA value where a specific condition is met, in your case y > 0.1*cos(x).
So, from AA you can get:

AA_masked = np.ma.array(AA.real, mask = (y > 0.1*cos(x)))

And then you can plot it:

CS = ax.contour(x, y, AA_masked.real)
ax.clabel(CS, inline=True, fontsize=10)

enter image description here

As a check you can also plot the specific threshold:

x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'red')

enter image description here

Complete Code

import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh, exp
from matplotlib import pyplot as plt


delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
x, y = np.meshgrid(X, Y)
I = 1j  # imaginary unit
AA = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*((1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-1.*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*exp((0.7071067812e-1*I)*cos(x)+1.414213562+.7071067812*I+0.7071067812e-1*cos(x))-(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*exp(-(0.7071067812e-1*I)*cos(x)-.7071067812*I-0.7071067812e-1*cos(x))+(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y+(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y+1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*y-4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-0.7071067812e-1*cos(x)-(0.7071067812e-1*I)*cos(x))*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(.9583581328*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-.8189270221*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(.8189270221*I)*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.9583581328*I)*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+.5835053242*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-.4986113867*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(.4986113867*I)*cosh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.5835053242*I)*cosh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))))


fig, ax = plt.subplots()

AA_masked = np.ma.array(AA.real, mask = (y > 0.1*cos(x)))
CS = ax.contour(x, y, AA_masked.real)
ax.clabel(CS, inline=True, fontsize=10)

x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'red')

ax.set_xlabel(r'x')
ax.set_ylabel(r'y')

plt.show()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement