gpt4 book ai didi

python - 如何旋转等高线图的标签?

转载 作者:行者123 更新时间:2023-12-04 13:37:29 27 4
gpt4 key购买 nike

我正在将一些数据绘制到等高线图中,标签不会跟随等高线并拦截其他等高线。这很奇怪,因为等高线图上用于分配标签的空间仍然存在。我正在使用 manual_locations 手动定位标签。

enter image description here

我不是 Python/matplotlib 专家,但我之前做过一些等高线图,从未遇到过这个问题。有谁知道可能会发生什么?

非常感谢!

代码:

import numpy as np
import matplotlib.pyplot as plt

def contours():

################
##### DATA #####
################
q_ = [0.05*i for i in list(range(6,21))]

wsbyq = [[2373.70, 2320.12, 2267.32, 2120.96, 1933.04, 1674.46, 1363.97, 1047.89, 736.34, 579.60],
[2736.59, 2676.32, 2598.13, 2454.85, 2185.93, 1914.33, 1565.92, 1129.16, 810.25, 631.83],
[3079.03, 3035.37, 2937.21, 2747.43, 2479.60, 2098.69, 1737.57, 1299.06, 857.47, 679.11],
[3359.21, 3315.58, 3240.09, 2943.55, 2688.86, 2300.32, 1899.51, 1380.36, 931.81, 709.92],
[3651.06, 3574.74, 3459.86, 3216.05, 2874.69, 2498.50, 1922.79, 1449.84, 1128.36, 737.17],
[3869.19, 3751.32, 3657.04, 3361.00, 3054.42, 2586.78, 2033.74, 1563.18, 1235.88, 727.55],
[4064.82, 3952.26, 3860.70, 3570.72, 3205.89, 2749.54, 2152.77, 1575.52, 1017.36, 762.20],
[4122.88, 4187.63, 3999.41, 3708.77, 3301.46, 2805.89, 2169.49, 1658.02, 1027.06, 739.98],
[4345.96, 4310.55, 4145.53, 3800.98, 3449.76, 2852.13, 2263.59, 1655.12, 1058.94, 744.53],
[4455.72, 4382.23, 4265.61, 3891.95, 3522.96, 3052.89, 2298.00, 1732.20, 1047.00, 724.61],
[4580.08, 4486.36, 4322.50, 4000.49, 3546.12, 3084.15, 2402.48, 1792.32, 1081.24, 733.62],
[4640.96, 4572.32, 4393.13, 4043.55, 3605.76, 3104.48, 2244.29, 1797.20, 1081.50, 711.26],
[4705.75, 4630.29, 4452.51, 4035.86, 3631.22, 3091.99, 2299.85, 1823.66, 1076.63, 689.89],
[4725.35, 4649.30, 4473.35, 4129.12, 3716.76, 3150.05, 2379.94, 1803.47, 1074.63, 684.22],
[4738.27, 4652.39, 4474.26, 4137.50, 3699.76, 3119.08, 2313.69, 1769.43, 1131.35, 692.20]]

w_ = [2082.6,2444.6,2802.5,3109.1,3385.8,3602.3,3800.6,3991.6,4133.5,4225.1,4343.0,4390.4,4445.4,4480.3,4479.8]

wb0 = [ws[0] for ws in wsbyq]
wb1 = [ws[1] for ws in wsbyq]
wb2 = [ws[2] for ws in wsbyq]
wb3 = [ws[3] for ws in wsbyq]
wb4 = [ws[4] for ws in wsbyq]
wb5 = [ws[5] for ws in wsbyq]
wb6 = [ws[6] for ws in wsbyq]
wb7 = [ws[7] for ws in wsbyq]
wb8 = [ws[8] for ws in wsbyq]
wb9 = [ws[9] for ws in wsbyq]

W = [wb0,wb1,wb2,wb3,wb4,wb5,wb6,wb7,wb8,wb9]
Q = q_
B = [10*i for i in list(range(0,10))]

Wprime = [w_,w_]

############
### PLOT ###
############

q, b = np.meshgrid(Q, B)
qprime, aprime = np.meshgrid(q_, [-0.01,0.01])

from matplotlib import gridspec

# set figure and subplots
fig = plt.figure()
gs = gridspec.GridSpec(2,1,height_ratios=[1,6])
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

# plotting things
lines = [i*100 for i in list(range(5, 46))]
lwdths = [1,.5,.5,.5,.5]*8 + [1]
ax1.contour(qprime,aprime, Wprime, lines, linewidths=lwdths)
P = ax2.contour(q,b, [[w-250 for w in l] for l in W], lines, linewidths=lwdths)
manual_locations = [(0.364,84.3),(0.461,73.6),(0.507,63.9),(0.561,56.9),(0.586,48.9),(0.639,40.8),(0.705,31.7),(0.787,21.6)]
ax2.clabel(P, inline=1, fontsize=9, fmt='%1.0f km/s', manual=manual_locations)

# figure formatting
ax1.set_xlim([0.3, 1])
ax2.set_xlim([0.3, 1])
ax1.set_ylim([-0.01, 0.01])
ax2.set_ylim([90.0, 0.0])
ax2.set_xlabel('Mass ratio (q)')
ax1.set_ylabel('unknown')
ax2.set_ylabel('Primary spin magnitude (a1)')
ax1.set_yticklabels([])
ax1.tick_params(axis='y', labelsize=0, length=0)

fig.suptitle('test')
plt.savefig('test')
plt.show()

contours()

最佳答案

只需将我的评论转换为答案即可验证

添加 use_clabeltext=True在你的第二个标签中:

ax2.clabel(P, inline=1, fontsize=9, fmt='%1.0f km/s', use_clabeltext=True)

关于python - 如何旋转等高线图的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60923851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com