Skip to content

Note

Click here to download the full example code

Introduction to Distributions

When you have a few chains and want to contrast them all with each other, you probably want a summary plot.

To show you how they work, let's make some sample data that all has the same average.

from chainconsumer import Chain, ChainConsumer, Truth, make_sample

# Here's what you might start with
df_1 = make_sample(num_dimensions=4, seed=1, randomise_mean=True)
df_2 = make_sample(num_dimensions=5, seed=2, randomise_mean=True)
print(df_1.head())

Out:

          A         B         C          D  log_posterior
0  1.165623  5.284409  8.374808  16.421808      -9.082802
1  0.336769  5.304098  8.714608  15.086368      -4.100887
2 -0.804581  6.031140  9.373729  14.695598      -3.213869
3 -1.012814  4.219814  9.947640  13.324333      -3.929196
4  0.956188  2.005424  6.705883  16.512909      -8.334207

Using distributions

# And now we give this to chainconsumer
c = ChainConsumer()
c.add_chain(Chain(samples=df_1, name="An Example Contour"))
fig = c.plotter.plot_distributions()

$A = -0.74^{+1.02}_{-1.00}$, $B = 4.8\pm 1.0$, $C = 9.44^{+0.97}_{-1.04}$, $D = 14.5\pm 1.0$

If you want the summary stats you'll need to keep it just one chain. And if you don't want them, you can pass summarise=False to the PlotConfig.

When you add a second chain, you'll see the summaries disappear.

c.add_chain(Chain(samples=df_2, name="Another contour!"))
c.add_truth(Truth(location={"A": 0, "B": 0}))
fig = c.plotter.plot_distributions(col_wrap=3, columns=["A", "B"])

plot 3 distributions

Total running time of the script: ( 0 minutes 1.468 seconds)

Download Python source code: plot_3_distributions.py

Download Jupyter notebook: plot_3_distributions.ipynb

Gallery generated by mkdocs-gallery