选择等高线图上的标签数量
Choose number of labels on contourplot
我查看了 docs and examples matplotlib 的等高线图,也许错过了我现在要问的内容。
我有等高线图:
%matplotlib inline
%pylab inline
import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh
import matplotlib
from matplotlib import pyplot as plt
# values x, y
delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
I = 1j # imaginary unit
x, y = np.meshgrid(X, Y)
# really huge expr.
AA = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*(exp(1.414213562+.7071067812*I+(0.7071067812e-1*I)*cos(x)+0.7071067812e-1*cos(x))*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))-1.*exp(-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x)-.7071067812*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))+(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(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)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(-(.8189270221*I)*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.9583581328*I)*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.4986113867*I)*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(.5835053242*I)*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.4986113867*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.5835053242*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+.8189270221*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-.9583581328*cos(0.7071067812e-1*cos(x))*cosh(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, 25)
ax.clabel(CS, inline=True, fontsize=10, manual=None)
x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'black')
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
plt.show()
效果很好,情节:
但我需要限制标签数量,不是轮廓数量。
所以,我需要得到所有 25 个轮廓,这是这里的第四个参数
CS = ax.contour(x, y, AA_masked.real, 25)
但要限制我的绘图中的位数。所以最后我想让每个让我们说我的情节上的第三个标签保持所有轮廓:
希望我把问题说清楚了。那么我应该传递哪个参数来限制 matplotlib 中 contourplot 上的标签数量?
非常感谢您!
编辑 1
可以用clabel来实现,但那样的话我需要知道标签的值。有时我不知道轮廓的真实值,所以这种方法对我来说不合适
正如 Jody Klymak 在评论部分中正确指出的那样,可以在 clabel 中指定要标记的级别,并且由于级别存储在 cs.levels
中,因此无需知道确切的值级别并添加 ax=clabel(cs, levels=cs.levels[::5])
就可以了。
所以工作代码是:
%matplotlib inline
%pylab inline
import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh
import matplotlib
from matplotlib import pyplot as plt
# values x, y
delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
I = 1j # imaginary unit
x, y = np.meshgrid(X, Y)
# really huge expr.
AA = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*(exp(1.414213562+.7071067812*I+(0.7071067812e-1*I)*cos(x)+0.7071067812e-1*cos(x))*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))-1.*exp(-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x)-.7071067812*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))+(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(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)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(-(.8189270221*I)*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.9583581328*I)*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.4986113867*I)*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(.5835053242*I)*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.4986113867*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.5835053242*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+.8189270221*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-.9583581328*cos(0.7071067812e-1*cos(x))*cosh(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, 25)
ax.clabel(CS, levels=CS.levels[::2], inline=True, fontsize=10, manual=None)
x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'black')
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
plt.show()
这给出了令人愉快的结果:
我查看了 docs and examples matplotlib 的等高线图,也许错过了我现在要问的内容。
我有等高线图:
%matplotlib inline
%pylab inline
import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh
import matplotlib
from matplotlib import pyplot as plt
# values x, y
delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
I = 1j # imaginary unit
x, y = np.meshgrid(X, Y)
# really huge expr.
AA = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*(exp(1.414213562+.7071067812*I+(0.7071067812e-1*I)*cos(x)+0.7071067812e-1*cos(x))*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))-1.*exp(-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x)-.7071067812*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))+(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(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)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(-(.8189270221*I)*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.9583581328*I)*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.4986113867*I)*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(.5835053242*I)*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.4986113867*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.5835053242*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+.8189270221*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-.9583581328*cos(0.7071067812e-1*cos(x))*cosh(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, 25)
ax.clabel(CS, inline=True, fontsize=10, manual=None)
x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'black')
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
plt.show()
效果很好,情节:
但我需要限制标签数量,不是轮廓数量。
所以,我需要得到所有 25 个轮廓,这是这里的第四个参数
CS = ax.contour(x, y, AA_masked.real, 25)
但要限制我的绘图中的位数。所以最后我想让每个让我们说我的情节上的第三个标签保持所有轮廓:
希望我把问题说清楚了。那么我应该传递哪个参数来限制 matplotlib 中 contourplot 上的标签数量?
非常感谢您!
编辑 1
可以用clabel来实现,但那样的话我需要知道标签的值。有时我不知道轮廓的真实值,所以这种方法对我来说不合适
正如 Jody Klymak 在评论部分中正确指出的那样,可以在 clabel 中指定要标记的级别,并且由于级别存储在 cs.levels
中,因此无需知道确切的值级别并添加 ax=clabel(cs, levels=cs.levels[::5])
就可以了。
所以工作代码是:
%matplotlib inline
%pylab inline
import numpy as np
from numpy import sqrt, cos, sin, sinh, cosh
import matplotlib
from matplotlib import pyplot as plt
# values x, y
delta = 0.025
X = np.arange(0, 6, delta)
Y = np.arange(-1, 0.2, delta)
I = 1j # imaginary unit
x, y = np.meshgrid(X, Y)
# really huge expr.
AA = (0.1743261076e-2-0.1743261076e-2*I)*sin(x)*(exp(1.414213562+.7071067812*I+(0.7071067812e-1*I)*cos(x)+0.7071067812e-1*cos(x))*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))-1.*exp(-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x)-.7071067812*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))+(3.887320974*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+(2.366831780*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(2.022482447*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(3.321756325*I)*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(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)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(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)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+(.6752336213*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-(4.022439224*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*y-(1.318639968*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*y-2.022482447*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))+2.366831780*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))+4.178862074*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))-1.318639968*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+4.022439224*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.6752336213*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*sinh(0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))-(4.178862074*I)*cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))*y-3.321756325*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+3.887320974*sinh(.7071067812*y+(.7071067812*I)*y-(0.7071067812e-1*I)*cos(x)-0.7071067812e-1*cos(x))*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x)))/(cosh((0.7071067812e-1+0.7071067812e-1*I)*(10.+cos(x)))*(-(.8189270221*I)*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.9583581328*I)*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-(.4986113867*I)*cos(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-(.5835053242*I)*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))+.4986113867*sin(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))-.5835053242*cos(0.7071067812e-1*cos(x))*sinh(0.7071067812e-1*cos(x))+.8189270221*sin(0.7071067812e-1*cos(x))*cosh(0.7071067812e-1*cos(x))-.9583581328*cos(0.7071067812e-1*cos(x))*cosh(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, 25)
ax.clabel(CS, levels=CS.levels[::2], inline=True, fontsize=10, manual=None)
x_threshold = np.linspace(0, 6, 1000)
y_threshold = 0.1*np.cos(x_threshold)
ax.plot(x_threshold, y_threshold, linestyle = '--', color = 'black')
ax.set_xlabel(r'x')
ax.set_ylabel(r'y')
plt.show()
这给出了令人愉快的结果: