简体   繁体   中英

location of contour label matplotlib for custom labels

I am trying to plot a series of ellipses as contours. How can I specify the value of parametric constant C on the corresponding ellipse.

import matplotlib.pyplot as plt
from numpy import arange, meshgrid

delta = 0.025
xrange = arange(-20.0, 20.0, delta)
yrange = arange(-20.0, 20.0, delta)
X, Y = meshgrid(xrange,yrange)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim(xmin=-6, xmax=6)
ax.set_ylim(ymin=-4, ymax=4)

# F is one side of the equation, G is the other
F = (X**2)/2.0+(Y**2)
for C in range(6,14,3):
    CS=plt.contour(X, Y, F - C, [0],label=str(C))

plt.show()

Is there a way to locate x, y coordinates of contour clabel and then replace the default values with custom value C. I don't want to take the help of mouse clicks.

To label the contours you would use clabel . Since in this case it seems that you want to directly label the contours by their value, there should not be any need to determine the coordinates or manipulate the labels in any way.

import matplotlib.pyplot as plt
from numpy import arange, meshgrid

delta = 0.025
x_range = arange(-20.0, 20.0, delta)
y_range = arange(-20.0, 20.0, delta)
X, Y = meshgrid(x_range,y_range)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim(xmin=-6, xmax=6)
ax.set_ylim(ymin=-4, ymax=4)

# F is one side of the equation, G is the other
F = (X**2)/2.0+(Y**2)

C = range(6,14,3)
CS = plt.contour(X, Y, F, C)
labels = plt.clabel(CS)

xy = [t.get_position() for t in labels]
print(xy)

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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