自定义标签的轮廓标签matplotlib的位置

时间:2018-01-29 10:15:54

标签: python matplotlib

我试图绘制一系列椭圆作为轮廓。如何在相应的椭圆上指定参数常数C的值。

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

有没有办法找到轮廓clabel的x,y坐标,然后用自定义值C替换默认值。我不想借助鼠标点击。

1 个答案:

答案 0 :(得分:1)

标记要使用的轮廓clabel。因为在这种情况下,您似乎想要按其值直接标记轮廓,所以不应该以任何方式确定坐标或操纵标签。

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

enter image description here