Power Spectrum Plotting module
pk
Power spectrum and posterior visualization functions.
This module provides utilities for plotting primordial power spectrum data, including posteriors, priors, and residuals from MCMC chains.
posteriors_delta(k, samples, colors=None, Nbins=20, kmin=0.001, kmax=0.34, figsize=(6, 8), chain_entries=None, **kwargs)
Plot posteriors for deviations only: \(1 + \delta(k)\).
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Array of \(k\) values
TYPE:
|
samples
|
Dict mapping labels to lists of samples (each sample is \(1 + \delta(k)\))
TYPE:
|
colors
|
List of colors for each dataset (optional, required if chain_entries not provided)
TYPE:
|
Nbins
|
Number of bins for the node visualization (default: 20)
TYPE:
|
kmin
|
Minimum k value (default: 1e-3)
TYPE:
|
kmax
|
Maximum k value (default: 0.34)
TYPE:
|
chain_entries
|
Optional ChainsCollection or list of ChainEntry objects for auto color extraction
TYPE:
|
kwargs
|
Additional keyword arguments for
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Figure
|
Matplotlib Figure object. |
Source code in src/primefeat/plots/pk.py
posteriors_PPS(k, samples=None, colors=None, mode='delta', ax=None, show_binning_range=False, k_start=None, k_end=None, alpha_contour=0.5, add_inset=False, inset_klim=(0.01, 0.2), inset_bbox=(0.55, 0.55, 0.4, 0.4), inset_ylim=None, sigma_levels=2, show_mean=False, normalize=1000000000.0, add_2nd_xaxis=True, fig_kw=None, chain_entries=None)
Plot posteriors for primordial power spectrum.
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Array of \(k\) values
TYPE:
|
samples
|
Dict mapping labels to lists of samples. If None and chain_entries is provided, samples will be extracted from the entries.
TYPE:
|
colors
|
List of colors for each dataset (optional). If not provided and chain_entries is None, will use registered colors from get_chains(), or auto-generated defaults. If chain_entries is provided, colors will be extracted from the entries. Supported formats: hex codes ('#2E86AB'), named colors ('red', 'blue'), or RGB tuples.
TYPE:
|
mode
|
"delta" for \(1+\delta(k)\) or "full" for full \(P(k) = A_s \cdot k^{(n_s-1)} \cdot [1+\delta(k)]\)
TYPE:
|
ax
|
Existing matplotlib axes (optional)
TYPE:
|
show_binning_range
|
If True, show gray shaded region for binning range
TYPE:
|
k_start
|
Start of binning range (only used if show_binning_range=True)
TYPE:
|
k_end
|
End of binning range (only used if show_binning_range=True)
TYPE:
|
alpha_contour
|
Transparency for confidence bands (default: 0.5)
TYPE:
|
add_inset
|
If True, add an inset plot with zoomed view
TYPE:
|
inset_klim
|
Tuple of (kmin, kmax) for inset zoom range
TYPE:
|
inset_bbox
|
Tuple of (x, y, width, height) for inset position in axes coordinates
TYPE:
|
inset_ylim
|
Tuple of (ymin, ymax) for inset y-axis limits (optional)
TYPE:
|
sigma_levels
|
Number of \(\sigma\) levels to show in contours (default: 2)
TYPE:
|
show_mean
|
If True, show mean instead of median (default: False)
TYPE:
|
normalize
|
Normalization factor (default: 1e9)
TYPE:
|
fig_kw
|
Additional keyword arguments for plt.subplots (e.g., figsize)
TYPE:
|
chain_entries
|
Optional ChainsCollection or list of ChainEntry objects. When provided, colors and labels are extracted automatically. For backward compatibility, this can be omitted and samples + colors passed explicitly.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Figure
|
Matplotlib Figure object. |
Source code in src/primefeat/plots/pk.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
priors_PPS(k, delta_prior, As_prior=None, ns_prior=None, n_samples=10000, k_pivot=0.05, sigma_levels=2, alpha_contour=0.5, normalize=1000000000.0, color='steelblue', label='Prior', ax=None, fig_kw=None, **line_kwargs)
Plot the prior distribution of the primordial power spectrum P(k).
Randomly draws n_samples parameter sets from the user-specified
priors, evaluates P(k) for each draw, then displays the resulting
distribution as sigma-level bands (identical style to
:func:posteriors_PPS).
Two modes are supported automatically:
- Delta mode (default) — plots :math:
1 + \delta(k)when neitherAs_priornorns_prioris supplied. - Full mode — plots
:math:
10^9 A_s (k/k_{\mathrm{pivot}})^{n_s-1} [1+\delta(k)]when at least one ofAs_prior/ns_prioris provided. Missing parameters are held fixed at their fiducial values (:math:A_s = 2.1 \times 10^{-9}, :math:n_s = 0.965).
Prior specifications
A prior spec is either:
(low, high)— draws from :math:\mathcal{U}(\text{low}, \text{high}).('normal', mean, std)— draws from :math:\mathcal{N}(\text{mean}, \text{std}^2).
For delta_prior you may supply a single spec applied to all
k-bins, or a list of specs with one entry per bin.
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Array of :math:
TYPE:
|
delta_prior
|
Prior specification for the :math:
TYPE:
|
As_prior
|
Optional prior for :math:
TYPE:
|
ns_prior
|
Optional prior for :math:
TYPE:
|
n_samples
|
Number of random draws from the prior (default: 10 000).
TYPE:
|
k_pivot
|
Pivot scale in Mpc :sup:
TYPE:
|
sigma_levels
|
Number of :math:
TYPE:
|
alpha_contour
|
Transparency of the confidence bands (default: 0.5).
TYPE:
|
normalize
|
Overall normalization factor (default: :math:
TYPE:
|
color
|
Color for the prior bands and median line (default:
TYPE:
|
label
|
Legend label (default:
TYPE:
|
ax
|
Existing matplotlib axes. When provided the function plots
directly onto this axes without setting up the dual-axis
canvas (no secondary :math:
TYPE:
|
fig_kw
|
Keyword arguments forwarded to
TYPE:
|
**line_kwargs
|
Additional keyword arguments forwarded to the median
line (e.g.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Matplotlib
|
class:
TYPE:
|
Examples:
>>> import numpy as np
>>> import primefeat.plots as pf_plot
>>> k = np.logspace(-3, 0, 200)
>>> # Prior bands on 1 + delta(k) with uniform prior on each bin
>>> fig = pf_plot.priors_PPS(k, delta_prior=(-0.5, 0.5))
>>> # Full P(k) prior including A_s and n_s
>>> fig = pf_plot.priors_PPS(
... k,
... delta_prior=(-0.3, 0.3),
... As_prior=(1.5, 2.5),
... ns_prior=('normal', 0.965, 0.02),
... sigma_levels=2,
... color='darkorange',
... )
Source code in src/primefeat/plots/pk.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
powerlaw_residuals(k, samples_Pk, colors=None, ax=None, alpha_contour=0.5, sigma_levels=2, show_binning_range=False, k_start=None, k_end=None, add_inset=False, inset_klim=(0.01, 0.2), inset_bbox=(0.55, 0.55, 0.4, 0.4), inset_ylim=None, show_nodes=False, nbins=20, node_marker='o', node_size=40, use_median=False, fig_kw=None, chain_entries=None)
Plot residuals with respect to a fiducial power-law power spectrum.
This function plots \(\delta(k)\), the fractional deviations from the best-fit power-law model \(P_{\mathrm{PL}}(k) = A_s \cdot (k/k_{\mathrm{pivot}})^{(n_s-1)}\). The residuals represent the primordial features in the power spectrum.
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Array of \(k\) values (wavenumbers) in Mpc\(^{-1}\).
TYPE:
|
samples_Pk
|
Dict mapping labels to arrays of \(\delta(k)\) samples. Each array should have shape (n_samples, n_k_bins). \(\delta(k)\) values represent fractional deviations from power-law.
TYPE:
|
colors
|
List of colors for each dataset (optional, required if chain_entries not provided).
TYPE:
|
ax
|
Existing matplotlib axes. If None, creates new figure.
TYPE:
|
alpha_contour
|
Transparency for confidence bands (default: 0.5).
TYPE:
|
sigma_levels
|
Number of \(\sigma\) levels for contours (1, 2, or 3).
TYPE:
|
show_binning_range
|
If True, shade extrapolation regions.
TYPE:
|
k_start
|
Start of binning range (for extrapolation shading).
TYPE:
|
k_end
|
End of binning range (for extrapolation shading).
TYPE:
|
add_inset
|
If True, add an inset plot with zoomed view.
TYPE:
|
inset_klim
|
Tuple (kmin, kmax) for inset zoom range.
TYPE:
|
inset_bbox
|
Tuple (x, y, width, height) for inset position.
TYPE:
|
inset_ylim
|
Tuple (ymin, ymax) for inset y-axis limits.
TYPE:
|
show_nodes
|
If True, plot node values at bin centers as scatter points.
TYPE:
|
nbins
|
Number of bins (used if show_nodes=True).
TYPE:
|
node_marker
|
Marker style for node points (default: 'o').
TYPE:
|
node_size
|
Size of node markers (default: 40).
TYPE:
|
use_median
|
If True, use median instead of mean for node values.
TYPE:
|
fig_kw
|
Keyword arguments passed to plt.subplots().
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Figure
|
Matplotlib Figure object. |
Examples:
>>> import primefeat.plots as plot
>>> fig = plot.powerlaw_residuals(
... k=k_centers,
... samples={"Planck": delta_samples_planck, "ACT": delta_samples_act},
... colors=["C0", "C1"],
... sigma_levels=2,
... )
Source code in src/primefeat/plots/pk.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
posteriors_ns_eff(k, ns_eff_samples, colors=None, chain_entries=None, ax=None, show_binning_range=False, k_start=None, k_end=None, alpha_contour=0.5, add_inset=False, inset_klim=(0.01, 0.2), inset_bbox=(0.55, 0.55, 0.4, 0.4), inset_ylim=None, fig_kw=None)
Plot posteriors for effective spectral index \(n_s(k) - 1 = d \ln P(k) / d \ln k\).
| PARAMETER | DESCRIPTION |
|---|---|
k
|
Array of \(k\) values
TYPE:
|
ns_eff_samples
|
Dict mapping labels to arrays of \(n_s(k) - 1\) samples
TYPE:
|
colors
|
List of colors for each dataset (optional if chain_entries provided)
TYPE:
|
chain_entries
|
ChainsCollection or list of ChainEntry objects; colors auto-extracted if provided
TYPE:
|
ax
|
Existing matplotlib axes (optional)
TYPE:
|
show_binning_range
|
If True, show gray shaded region for binning range
TYPE:
|
k_start
|
Start of binning range (only used if show_binning_range=True)
TYPE:
|
k_end
|
End of binning range (only used if show_binning_range=True)
TYPE:
|
alpha_contour
|
Transparency for confidence bands (default: 0.5)
TYPE:
|
add_inset
|
If True, add an inset plot with zoomed view
TYPE:
|
inset_klim
|
Tuple of (kmin, kmax) for inset zoom range
TYPE:
|
inset_bbox
|
Tuple of (x, y, width, height) for inset position in axes coordinates
TYPE:
|
inset_ylim
|
Tuple of (ymin, ymax) for inset y-axis limits (optional)
TYPE:
|
fig_kw
|
Additional keyword arguments for plt.subplots (e.g., figsize)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Figure
|
Matplotlib Figure object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither colors nor chain_entries is provided. |
Source code in src/primefeat/plots/pk.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
posteriors_delta_pcolormesh(chain_dict, nbins=20, n_grid_points=100, delta_range=(-0.5, 0.5), cmap='viridis', figsize=(10, 6), log_density=True, title=None)
Plot posterior distributions for all delta_i bins as a 2D pcolormesh density map.
Creates a 2D histogram/density visualization where: - X-axis: bin index (delta_1, delta_2, ..., delta_nbins) - Y-axis: parameter value (delta) - Color: probability density from posterior samples
| PARAMETER | DESCRIPTION |
|---|---|
chain_dict
|
Dictionary mapping bin indices to samples, or a GetDist MCSamples object with delta_i parameters
TYPE:
|
nbins
|
Number of delta bins (default: 20)
TYPE:
|
n_grid_points
|
Resolution for density grid (default: 100)
TYPE:
|
delta_range
|
Tuple of (min, max) for y-axis delta range. Default: (-0.5, 0.5) based on flat prior. Set to None to infer from data with 10% padding.
TYPE:
|
cmap
|
Colormap name (default: 'viridis')
TYPE:
|
figsize
|
Figure size (default: (10, 6))
TYPE:
|
log_density
|
If True, show log10(density) for better visibility (default: True)
TYPE:
|
title
|
Figure title (optional)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[Figure, Axes]
|
Tuple of (fig, ax) matplotlib objects |
Examples:
>>> import primefeat as pf
>>> chains = pf.get_chains(1e-3)
>>> fig, ax = pf.plot.posteriors_delta_pcolormesh(chains['PR3'], nbins=20)
>>> plt.show()
>>> # Multiple datasets in subplots
>>> fig, axs = plt.subplots(1, len(chains), figsize=(12, 5))
>>> for ax, (label, chain) in zip(axs, chains.items()):
... pf.plot.posteriors_delta_pcolormesh(chain, nbins=20, figsize=(4, 5))
Source code in src/primefeat/plots/pk.py
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 | |