如何旋转轮廓图的标签?

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

我正在将一些数据绘制到等高线图中,标签将不会跟随等高线并截取其他等高线。这很奇怪,因为等高线图上用于分配标签的空间仍然存在。我正在使用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()
python matplotlib contour
1个回答
1
投票

只需变换我的评论以进行验证

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

ax2.clabel(P, inline=1, fontsize=9, fmt='%1.0f km/s', use_clabeltext=True)
© www.soinside.com 2019 - 2024. All rights reserved.