简体   繁体   English

Matplotlib 带可变上限

[英]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所有的共同点在此处输入图像描述

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问题是我需要 plot 它假设我的 y 不是一个简单的数组并且上限是可变的并且必须遵守

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

In that case the result has to be在这种情况下,结果必须是

在此处输入图像描述

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!非常感谢您!

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) .您可以使用屏蔽数组来屏蔽满足特定条件的AA值,在您的情况下y > 0.1*cos(x)
So, from AA you can get:所以,从AA你可以得到:

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

And then you can plot it:然后你可以 plot 它:

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

在此处输入图像描述

As a check you can also plot the specific threshold:作为检查,您还可以 plot 具体阈值:

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

在此处输入图像描述

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM