我正在使用
matplotlib
制作等高线图,而 clabel
生成的标签太靠近图形边界。 如何使它们更好地定位?我尝试了 manual
arg,但由于我有来自 for 循环的多个轮廓,因此很难预先确定每次放置的位置。我用来绘制的代码是
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test contours
"""
# %%
import numpy as np
import matplotlib.pyplot as plt
g = 9.8
rhoh = 929 # hydrate density
rhol = 1029 # seawater density
rhos = 2650 # dry sediment density
psi = 25 # constant friction angle °
H = 137.95 # min depth of the hydrate layer m
D = 17.6 # max thickness of the hydrate layer m
nu = [0.15, 0.45] # min and max Poisson's ratio
# ----- Constants ----- #
def weight(phi=0.483):
gammas = rhos * g # unit weight of dry soil
gammal = rhol * g # unit weight of seawater
return gammal, (gammas - gammal) * (1 - phi)
# ----- Critical stress ----- #
def normal(H, beta):
beta = np.deg2rad(beta)
_, gammap = weight()
return H * gammap * np.cos(beta) ** 2
def slip(H, beta, nu, E):
sigma0 = normal(H, beta)
G = E / 2 / (1 + nu)
df = np.tan(np.deg2rad(psi))
du_slip = sigma0 - 0.579 * G / 100 / df / (1 - nu)
du_slip[du_slip < 0] = np.nan
return du_slip
def mesh(N):
Hd = H + np.linspace(0, D, N)
nuu = np.linspace(nu[0], nu[1], N)
Hd, nnuu = np.meshgrid(Hd, nuu)
return Hd, nnuu
E = 70e6
beta = [5, 10, 15]
N = 1000
Hd, nu = mesh(N)
du_slip = []
for i in range(len(beta)):
du_slip.append(slip(Hd, beta[i], nu, E))
styles = ["-", "--", ":", "-."]
colors = ["k", "b", "r", "g"]
levels = np.arange(0.5, 0.9, 0.1)
for i in range(len(beta)):
c = plt.contour(
Hd - H,
nu,
du_slip[i] / 1e6,
levels,
colors=colors[i],
linestyles=styles[i],
linewidths=1,
)
# my current hack
cl = plt.clabel(c, levels, fontsize=8, use_clabeltext=True)
# for l in cl:
# # move the location of the label to avoid overlapping
# l.set_position(
# (
# l.get_position()[0] - 1,
# l.get_position()[1]
# - 0.005
# * np.tan(l.get_rotation() * np.pi / 180), # 0.005 from trial and error
# )
# )
plt.plot([], [], styles[i], color="k", label=rf"$\beta$={beta[i]}°")
plt.legend(loc="lower left", frameon=True)
plt.xlabel(r"$D$ (m)", fontsize=12)
plt.ylabel(r"$\nu$", rotation="horizontal", fontsize=12, labelpad=15)
plt.title(r"$\Delta u_\mathrm{slip}$ (MPa)", fontsize=12)
plt.savefig("test1.png", bbox_inches="tight")
# %%
我当前的技巧是获取每个标签的位置和方向,并将它们沿着线向下移动一点
cl = plt.clabel(c, levels, inline=False, fontsize=8, use_clabeltext=True)
for l in cl:
# move the location of the label to avoid overlapping
l.set_position(
(
l.get_position()[0] - 1,
l.get_position()[1]
- 0.005
* np.tan(l.get_rotation() * np.pi / 180), # 0.005 from trial and error
)
)
如有任何帮助,我们将不胜感激!
clabel
允许手动定位标签:
plt.clabel(contours, inline=True, fmt="%1.1f", fontsize=10, manual=True)
其中
contours
是 plt.contour(...)
返回的对象。
运行脚本将打开一个包含绘图的窗口,系统将提示您左键单击所需每个标签的位置。然后,当您对位置感到满意时,可以右键单击关闭窗口。