John
John

Reputation: 475

Choose number of labels on contourplot

I looked up through docs and examples of contour plot of matplotlib and perhaps missed what I am asking now.

I have a contour plot:

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

that is working great and plots:

enter image description here

But I need to limit number of labels, not number of contours.

So, I need to get all 25 contours which is fourth parameters here

CS = ax.contour(x, y, AA_masked.real, 25)

but to limit numbers of digits on my plot. So in the end I would like to get each let's say third label on my plot keeping all contours:

enter image description here

Hopefully, I stated my issue clear. So which parameter should I pass to limit number of labels on contourplot in matplotlib?

Thank you very much in advance!

EDIT 1

It is possible to achieve it with clabel, but in that case I need to know the values of labels. Sometimes I do not know the real values of contour so that approach is not okay for me

Upvotes: 0

Views: 413

Answers (1)

John
John

Reputation: 475

As Jody Klymak correctly stated in the comments sections, one may specify the levels to be labeled in clabel and since the levels are stored in cs.levels there is no need to know the exact values of levels and adding ax=clabel(cs, levels=cs.levels[::5]) does the job.

So the working code is:

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

which gives the pleasant outcome: enter image description here

Upvotes: 1

Related Questions