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

问题描述 投票:0回答:1

我正在尝试绘制一系列椭圆作为轮廓。如何指定相应椭圆上参数常数 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替换默认值。我不想借助鼠标点击。

python matplotlib label contour
1个回答
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()

© www.soinside.com 2019 - 2024. All rights reserved.