Change both x and y labels with Latex symbols instead of p1, p2, p3 etc...

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
Dournac Fabien
Posts: 74
Joined: May 18 2019
Affiliation: IRAP
Contact:

Change both x and y labels with Latex symbols instead of p1, p2, p3 etc...

Post by Dournac Fabien » April 04 2020

Hello,

I have a covariance matrix deduced Fisher formalism and I need to plot it in a triplot (joint distribution between the 7 cosmological parameters).

By default, the parameters have the names "p1", "p2", "p3", ..., "p7" and I would like to replace them by Latex symbol :

[math]

Here below the script I am using :

Code: Select all

# Show plots inline, and load main getdist plot module and samples class
from __future__ import print_function
import sys, os
sys.path.insert(0,os.path.realpath(os.path.join(os.getcwd(),'..')))
import numpy as np
from getdist import plots
import getdist
import matplotlib.pyplot as plt
print('GetDist Version: %s, Matplotlib version: %s'%(getdist.__version__, plt.matplotlib.__version__))

# The plotting scripts also let you plot Gaussian (or Gaussian mixture) contours 
from getdist.gaussian_mixtures import GaussianND
## Load Fisher matrix and invert it to get Covariance array
# Load Fisher matrix
Fish_GCs_GCp_WL = np.loadtxt('Fisher_GCs_GCp_WL')
Fish_GCs_GCp_WL_XC = np.loadtxt('Fisher_GCs_GCp_WL_XC')
# Invert to get Covariance matrix
COV_Fish_GCs_GCp_WL = np.linalg.inv(Fish_GCs_GCp_WL)[0:8,0:8]
COV_Fish_GCs_GCp_WL_XC = np.linalg.inv(Fish_GCs_GCp_WL_XC)[0:8,0:8]
mean = [0.05, 0.67, 0.32, 0.96, 0.68, -1, 0, 0.815] 
#names = ['$\Omega_{b}$', '$h$', '$\Omega_{m}$', '$n_{s}$', '$\Omega_{DE}$', '$w_{0}$',\

labels = np.copy(names)
matrix1 = GaussianND(mean, COV_Fish_GCs_GCp_WL)
matrix2 = GaussianND(mean, COV_Fish_GCs_GCp_WL_XC)
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.scaling = False
g.settings.alpha_filled_add=0.95
g.settings.title_limit_fontsize = 18
g.settings.legend_fontsize = 18
#names4 = g.settings.param_latex_label(None, names2[0])

plt.rcParams['text.usetex']=True
names = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']
labels = [r'\Omega_{m}', r'\Omega{b}', r'w_{0}', r'w_{a}$', r'h', r'n_{s}', r'\sigma_{8}']
g.triangle_plot([matrix1, matrix2], labels = labels, names = names, filled = True)
#g.triangle_params =['param1', 'param2', 'param3', 'param4', 'param5',\
#                'param6', 'param7'], labels=[r'\Omega_{m}', r'\Omega{b}', r'w_{0}', \
#		r'w_{a}$', r'h', r'n_{s}', r'\sigma_{8}'], label='Model')
#names = [param1, param2, param3, param4, param5,\
#                param6, param7]
#labels = [r'\Omega_{m}', r'\Omega{b}', r'w_{0}', \
#	 r'w_{a}$', r'h', r'n_{s}', r'\sigma_{8}']
g.export('output_example.pdf')
Any track/clue/suggestion is welcome, Regards
Last edited by Dournac Fabien on April 06 2020, edited 2 times in total.

Antony Lewis
Posts: 1941
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: Change both x and y labels with Latex symbols instead of p1, p2, p3 etc...

Post by Antony Lewis » April 04 2020

You can pass a labels argument to the GaussianND constructor with list of latex labels (also a names argument to call them something other than p1, p2). See e.g. the example in box 40 of the notebook

https://getdist.readthedocs.io/en/latest/plot_gallery.html

Dournac Fabien
Posts: 74
Joined: May 18 2019
Affiliation: IRAP
Contact:

Re: Change both x and y labels with Latex symbols instead of p1, p2, p3 etc...

Post by Dournac Fabien » April 04 2020

Thanks Antony,

However, I have a problem with the variable 'mix_samples'. Indeed, I don't use GetDist to plot MCMC chains but only for plotting the joint distribution between parameters, what we call commonly a "triplot". Here the item 40 from the link you gave me :

Code: Select all

mixture=Mixture2D([mean1, mean2], [cov1, cov2], names=['zobs','t'], labels=[r'z_{\rm obs}', 't'], label='Model')

# Generate samples from the mixture as simple example
mix_samples = mixture.MCSamples(3000, label='Samples')

g = plots.get_subplot_plotter()
# compare the analytic mixture to the sample density
g.triangle_plot([mix_samples, mixture], filled=False)
and my code with 2 Covariance matrixes :

Code: Select all

# Mean of each cosmo parameters
mean = [0.32, 0.05, -1.0, 0.0, 0.67, 0.96, 0.815]
#matrix1 = GaussianND(mean, COV_Fish_GCs_GCp_WL)
#matrix2 = GaussianND(mean, COV_Fish_GCs_GCp_WL_XC)
matrix1 = COV_Fish_GCs_GCp_WL
matrix2 = COV_Fish_GCs_GCp_WL_XC

# Plot joint distributions (triplot)
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.scaling = False
g.settings.alpha_filled_add=0.95
g.settings.title_limit_fontsize = 18
g.settings.legend_fontsize = 18
plt.rcParams['text.usetex']=True

# Names and labels
names = ['Omega_m', 'Omega_b', 'w_0', 'w_a', 'h', 'n_s', 'sigma_8']
labels = [r'\Omega_{m}', r'\Omega{b}', r'w_{0}', r'w_{a}$', r'h', r'n_{s}', r'\sigma_{8}']

# Create mixture for covariance and labels
mixture=Mixture2D([mean, mean], [matrix1, matrix2], names=names, labels=labels, label='Model')

# Plot triplot
g.triangle_plot([None, mixture], filled=True)
# Save triplot
g.export('output_example.pdf')
As you can see, I have set None into the line of code : g.triangle_plot([None, mixture], filled=True)
: it seems None attribute is not accepted in this case.

But at the execution, I get the following error :

Code: Select all

Traceback (most recent call last):
  File "example2_for.py", line 41, in <module>
    g.triangle_plot([None, mixture], filled=True)
  File "/Users/user/opt/anaconda2/envs/ipy2/lib/python2.7/site-packages/getdist/plots.py", line 2301, in triangle_plot
    params = self.get_param_array(roots[0], params)
  File "/Users/user/opt/anaconda2/envs/ipy2/lib/python2.7/site-packages/getdist/plots.py", line 1744, in get_param_array
    names = self.param_names_for_root(root)
  File "/Users/user/opt/anaconda2/envs/ipy2/lib/python2.7/site-packages/getdist/plots.py", line 884, in param_names_for_root
    self.sample_analyser.params_for_root(root, label_params=self.settings.param_names_for_labels)
  File "/Users/user/opt/anaconda2/envs/ipy2/lib/python2.7/site-packages/getdist/plots.py", line 621, in params_for_root
    samples = self.samples_for_root(root)
  File "/Users/user/opt/anaconda2/envs/ipy2/lib/python2.7/site-packages/getdist/plots.py", line 468, in samples_for_root
    raise GetDistPlotError('Root names must be strings (or MCSamples instances)')
getdist.plots.GetDistPlotError: Root names must be strings (or MCSamples instances):
I have no idea of what to set for the first argument concerned in g.triangle_plot function.

If someone could suggest me a clue/track/remark about this issue.

Regards

Dournac Fabien
Posts: 74
Joined: May 18 2019
Affiliation: IRAP
Contact:

Re: Change both x and y labels with Latex symbols instead of p1, p2, p3 etc...

Post by Dournac Fabien » April 05 2020

Finally fixed it this way :

Code: Select all

# Names and labels
names = ['Omega_m', 'Omega_b', 'w_0', 'w_a', 'h', 'n_s', 'sigma_8']
labels = [r'\Omega_{m}', r'\Omega_{b}', r'w_{0}', r'w_{a}', r'h', r'n_{s}', r'\sigma_{8}']
matrix1 = GaussianND(mean, COV_Fish_GCs_GCp_WL, labels = labels, names = names)
matrix2 = GaussianND(mean, COV_Fish_GCs_GCp_WL_XC, labels = labels, names = names)
# Plot triplot
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.scaling = False
g.settings.alpha_filled_add=0.95
g.settings.title_limit_fontsize = 18
g.settings.legend_fontsize = 18
plt.rcParams['text.usetex']=True
g.triangle_plot([matrix1, matrix2], filled=True)
# Save triplot
g.export('output_example.pdf')

Post Reply