如何在Python中为等高线图添加标签

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

我正在尝试向我的轮廓图添加标签,例如所有 10 个轮廓图的 WD1、WD2、WD3、WD4、WD5、...、WD10。 作为示例,我只添加了两个等高线图命令。

# My code
import numpy as np
import matplotlib.pyplot as plt

def C2thin1(mx_mesh, sigma_mesh):
    return Cthin1(np.column_stack((mx_mesh.ravel(), sigma_mesh.ravel()))).reshape(mx_mesh.shape)

def C2thin2(mx_mesh, sigma_mesh):
    return Cthin2(np.column_stack((mx_mesh.ravel(), sigma_mesh.ravel()))).reshape(mx_mesh.shape)

def cage1_contour(C2thin_func, mx_mesh, sigma_mesh, Rstar, dstar):
    return e2dPhidE(C2thin_func, mx_mesh, sigma_mesh, Rstar, dstar)

# Define the range of mx and sigma values
mx_values = np.logspace(np.log10(0.01), np.log10(1000), 100)
sigma_values = np.logspace(np.log10(1e-30), np.log10(1e-50), 100)

# Create a meshgrid for contour plotting
mx_mesh, sigma_mesh = np.meshgrid(mx_values, sigma_values)

# Compute contour values for each function
contour_values1 = cage1_contour(C2thin1, mx_mesh, sigma_mesh, Rstar1, dstar1)
contour_values2 = cage1_contour(C2thin2, mx_mesh, sigma_mesh, Rstar2, dstar2)
# Condition to create the contour line
condition_values1 = sen9int(mx_mesh) - contour_values1
condition_values2 = sen9int(mx_mesh) - contour_values2
# Plot the contours
plt.figure(figsize=(10, 10))

contour1 = plt.contour(mx_mesh, sigma_mesh, condition_values1, levels=[0], colors='purple', linewidths=3, label='WD1')
contour2 = plt.contour(mx_mesh, sigma_mesh, condition_values2, levels=[0], colors='blue', linewidths=3, label='WD2')
plt.xscale('log')
plt.yscale('log')
plt.xlabel(r'$m_{\chi}$ (GeV)', fontsize=24, fontweight='bold')
plt.ylabel(r'$\sigma_{\chi n}$ (cm$^2$)', fontsize=24, fontweight='bold')
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.grid(True, which='both', linestyle='--', linewidth=2, color='black')
plt.axhline(y=1e-41, color='black', linestyle='--', linewidth=2)
plt.axvline(x=100.4, color='black', linestyle='--', linewidth=2)
plt.title('Contour Plot', fontsize=24, fontweight='bold')
plt.legend(fontsize=18)
plt.show()
```[enter image description here](https://i.sstatic.net/IrHd2FWk.png)
python matplotlib plot label contour
1个回答
0
投票

用 WD1、WD2、WD3 等标记每个等高线图,您可以迭代每个等高线图并在所需坐标处添加文本标签。

import numpy as np
import matplotlib.pyplot as plt

def C2thin1(mx_mesh, sigma_mesh):
    return Cthin1(np.column_stack((mx_mesh.ravel(), sigma_mesh.ravel()))).reshape(mx_mesh.shape)

def C2thin2(mx_mesh, sigma_mesh):
    return Cthin2(np.column_stack((mx_mesh.ravel(), sigma_mesh.ravel()))).reshape(mx_mesh.shape)

def cage1_contour(C2thin_func, mx_mesh, sigma_mesh, Rstar, dstar):
    return e2dPhidE(C2thin_func, mx_mesh, sigma_mesh, Rstar, dstar)

mx_values = np.logspace(np.log10(0.01), np.log10(1000), 100)
sigma_values = np.logspace(np.log10(1e-30), np.log10(1e-50), 100)

mx_mesh, sigma_mesh = np.meshgrid(mx_values, sigma_values)

contour_values1 = cage1_contour(C2thin1, mx_mesh, sigma_mesh, Rstar1, dstar1)
contour_values2 = cage1_contour(C2thin2, mx_mesh, sigma_mesh, Rstar2, dstar2)
condition_values1 = sen9int(mx_mesh) - contour_values1
condition_values2 = sen9int(mx_mesh) - contour_values2
plt.figure(figsize=(10, 10))

contour1 = plt.contour(mx_mesh, sigma_mesh, condition_values1, levels=[0], colors='purple', linewidths=3, label='WD1')
contour2 = plt.contour(mx_mesh, sigma_mesh, condition_values2, levels=[0], colors='blue', linewidths=3, label='WD2')

for level, contour in zip(range(1, 11), [contour1, contour2]):
    plt.text(contour.allsegs[level-1][0][0, 0], contour.allsegs[level-1][0][0, 1], f'WD{level}', fontsize=12, color='black')

plt.xscale('log')
plt.yscale('log')
plt.xlabel(r'$m_{\chi}$ (GeV)', fontsize=24, fontweight='bold')
plt.ylabel(r'$\sigma_{\chi n}$ (cm$^2$)', fontsize=24, fontweight='bold')
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.grid(True, which='both', linestyle='--', linewidth=2, color='black')
plt.axhline(y=1e-41, color='black', linestyle='--', linewidth=2)
plt.axvline(x=100.4, color='black', linestyle='--', linewidth=2)
plt.title('Contour Plot', fontsize=24, fontweight='bold')
plt.legend(fontsize=18)
plt.show()

只需根据绘图需要调整字体大小、颜色和位置。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.