在 Python 上将 2 组数据绘制到一张图上

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

这是我的 python 代码,用于在同一组轴上绘制 2 组代码。我需要在同一张图表上使用模拟代码和原始代码,以便我可以看到它们的拟合程度。

import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits


file_path = 'GalCat_extra_lean.fits'
hdul = fits.open(file_path)
data = hdul[1].data


halo_mass = data['mass_halo']
nsat = data['N_sat']

hdul.close()

# Define the halo mass and the mean number of satellites
selected_halo_mass = 1e12  # Selected halo mass in solar masses
mean_nsat = 20  # Mean number of satellites

# Generate simulated measurements using a Poisson sampler
np.random.seed(42)  # for reproducibility
simulated_nsat = np.random.poisson(mean_nsat, size=len(halo_mass)).astype(float)  # Convert to float
mask = halo_mass != selected_halo_mass
simulated_nsat[mask] = np.nan  # Set values to NaN where halo mass is not equal to the selected halo mass

# Plot the scatter plot with original data
plt.figure(figsize=(10, 6))
plt.scatter(halo_mass, nsat, color='blue', alpha=0.5, label='Original Data', s=0.2)
plt.xscale('log')
plt.yscale('log')
plt.xlabel('Halo Mass [$M_☉$]')
plt.ylabel('Number of Satellites ($N_{sat}$)')
plt.title('Number of Satellites vs. Halo Mass (log-log)')
plt.grid(True, which="both", ls="--")

# Plot the simulated data as a scatter plot with a different marker style or color
plt.scatter(halo_mass, simulated_nsat, color='red', alpha=0.5, label='Simulated Data', s=0.2, marker='x')

plt.legend()
plt.show()

我的剧情

我无法让模拟数据显示在我的绘图上 我该怎么做?

python physics astronomy
1个回答
0
投票

您是否尝试过增加模拟图的宽度?似乎被大量的实验数据点所掩盖了

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