Samreay / ChainConsumer

Corner plots, LaTeX tables and plotting walks.
https://samreay.github.io/ChainConsumer
MIT License
81 stars 18 forks source link

legend not showing and axis labels not using latex format #139

Closed margaret-ikape closed 2 months ago

margaret-ikape commented 3 months ago

Hi, I'm trying to make a distribution plot with the code below

import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['font.family'] = 'serif'

fixed_cosmo_mmin_zeta_planck_prior = pd.read_csv('output/fixed_cosmo_plancktauprior.txt', sep = "\s+", header=None, comment='#', skiprows=0)
fixed_cosmo_mmin_zeta_planck_prior.columns = ['a_tsz', 'a_ksz', 'xi', 'a_d', 'a_s2', 'a_sw', 'a_c', 'a_gd', 'a_gw', 'a_tps', 'a_ps', 'a_gted', 'a_gtew', 'a_geed', 'a_geew', 'beta_c', 'yp1', 'yp2', 'zeta_ksz', 'mmin_ksz', 'DLKSZ_3000', 'DL_3000', 'TAU_KSZ', 'TAU_TOT', 'DZ_75', 'Z_90', 'ZBAR_KSZ', 'prior', 'post']

actTT_planck_prior_mmin_zeta = pd.read_csv('output/free_mmin_zeta_plancktauprior.txt', sep = "\s+", header=None, comment='#')   
actTT_planck_prior_mmin_zeta.columns = ['a_tsz', 'a_ksz', 'xi', 'a_d', 'a_s2', 'a_sw', 'a_c', 'a_gd', 'a_gw', 'a_tps', 'a_ps', 'a_gted', 'a_gtew', 'a_geed', 'a_geew', 'beta_c', 'yp1', 'yp2', 'omch2', 'ombh2', 'cosmomc_theta', 'n_s', 'a_s', 'mmin_ksz', 'zeta_ksz', 'H0', 'SIGMA_8', 'DLKSZ_3000', 'DL_3000', 'TAU_KSZ', 'TAU_TOT', 'DZ_75', 'Z_90', 'ZBAR_KSZ', 'prior', 'post']

# Create ChainConsumer instance
c = ChainConsumer()

# Add chain from your actual data
c.add_chain(Chain(samples=fixed_cosmo_mmin_zeta_planck_prior[['mmin_ksz', 'TAU_KSZ', 'DZ_75', 'ZBAR_KSZ']], name="ACT C$_\ell^{TT}$ + G-P + Planck $\\tau$ prior (fixed)", color='#D62728', linestyle='--', kde=2.0))
c.add_chain(Chain(samples=actTT_planck_prior_mmin_zeta[['mmin_ksz', 'TAU_KSZ', 'DZ_75', 'ZBAR_KSZ']], name="ACT C$_\ell^{TT}$ + G-P + Planck $\\tau$ prior (free)", color='#D62728', kde=2.0))

c.set_plot_config(PlotConfig(
        legend_font_size=50,
        labels={'mmin_ksz':"Log $M_{min}$", 'TAU_KSZ':"$\\tau$", 'DZ_75':"$\Delta_z$", 'ZBAR_KSZ':r"$\bar{z}$"},
        shade=False,  # Turn off shading
        contour_label_font_size=50))

for ax in fig.get_axes():
    ax.tick_params(labelsize=20)
    ax.xaxis.label.set_size(30)
    ax.yaxis.label.set_size(30)
# Plot contours
fig = c.plotter.plot_distributions()
fig.set_size_inches(5 + fig.get_size_inches())

# Show the plot
# plt.show()
plt.savefig('fixfree.png', dpi=400)

However, the legend doesn't show up and the axis labels are not in latex even though I have specified the latex formats. fixfree

Samreay commented 3 months ago

Hi Margaret! Unfortunately the legend plotting was something I only implemented for the contour plots, not for the distributions. You can still add it manually, of course. If you want to enable latex, PlotConfig has a usetex parameter you can set to true. On that note, PlotConfig doesnt take legend_font_size or shade (shade would go in the Chain themselves). Similarly in your example I notice you're referencing fig (for ax in fig.get_axes()) before you define it in the line below the for loop, which may cause some odd behaviour if you're operating on a prior figure reference.

margaret-ikape commented 2 months ago

Thank you very much for your response, and for catching the figreferencing. Indeed, I was using a jupyter notebook and had previously defined fig. It is fixed now. Thank you again