# How to plot matrix plot of correlations in pymc3?

**URL:** <https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873>\
**Category:** Questions\
**Created:** [March 8, 2019, 6:45am UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873 "2019-03-08T06:45:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hoaithanhbk\_281113](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hoaithanhbk_281113/32/4603_2.png) [@Hoaithanhbk\_281113](https://discourse.pymc.io/u/Hoaithanhbk_281113)\
**Post date:** [March 8, 2019, 6:45am UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/1 "2019-03-08T06:45:02Z")

</div>

Hi everyone,  
I am new user pymc3. I don’t know how to plot matrix of correlations in pymc3?  
please help me!!  
thanks you

---

<div class="post-metadata">

**Author:** ![lwahedi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lwahedi/32/1441_2.png) [@lwahedi](https://discourse.pymc.io/u/lwahedi)\
**Post date:** [March 8, 2019, 12:10pm UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/2 "2019-03-08T12:10:32Z")

</div>

What exactly are you trying to correlate? It would help if you could be more specific, framed your problem and listed out what you’d tried and where you’re stuck.

If you’re trying to look at correlations in the traces, you can extract them with trace.get\_values, you specify the chains and it will give you a numpy array to do with what you want.

---

<div class="post-metadata">

**Author:** ![lucianopaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lucianopaz/32/2186_2.png) [@lucianopaz](https://discourse.pymc.io/u/lucianopaz)\
**Post date:** [March 8, 2019, 4:44pm UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/3 "2019-03-08T16:44:19Z")

</div>

My personal favorite is pairplot. You can try with [arviz](https://arviz-devs.github.io/arviz/notebooks/Introduction.html) or [seaborn](https://seaborn.pydata.org/tutorial/distributions.html). If you just want to see the Pearson correlation as a matrix you can use `numpy.corr` and `pyplot.pcolor`. If you need a but more help getting those to run, just ask

---

<div class="post-metadata">

**Author:** ![Hoaithanhbk\_281113](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hoaithanhbk_281113/32/4603_2.png) [@Hoaithanhbk\_281113](https://discourse.pymc.io/u/Hoaithanhbk_281113)\
**Post date:** [March 9, 2019, 4:33am UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/4 "2019-03-09T04:33:50Z")

</div>

my problem:  
I have three coefficients with prior uniform distribution:  
`C_1 (0.054,0.066); C_2 (0.9,1.1); C_3 (45,55)`

I have surrogate model of three coefficients:  
`mu = -1.2*c_1^2+0.01*c_1*c_2+0.05*c2^2-0.0004*c_1*c_3+0.16*c_1-0.0001*c_2*c_3+2.01*c3^2-0.002*c_2-4.03*c_3+0.01`

I want to find the correlations of three coefficients with mu and the posterior of mu.

please help me.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 9, 2019, 6:49am UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/5 "2019-03-09T06:49:24Z")

</div>

You can do prior sample and posterior sample of C\_1, C\_2, C\_3, and mu, and compute correlation between each pair between C\_1, C\_2, C\_3, and mu

---

<div class="post-metadata">

**Author:** ![Hoaithanhbk\_281113](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/hoaithanhbk_281113/32/4603_2.png) [@Hoaithanhbk\_281113](https://discourse.pymc.io/u/Hoaithanhbk_281113)\
**Post date:** [March 11, 2019, 1:44pm UTC](https://discourse.pymc.io/t/how-to-plot-matrix-plot-of-correlations-in-pymc3/2873/6 "2019-03-11T13:44:57Z")

</div>

this is my code :

> a= -1.24  
> b=0.011  
> c=0.005  
> d=-0.0004  
> e=0.16  
> f=-0.00013  
> g=-2.01e-6  
> k=-0.002  
> m=-4.03e-5  
> l=0.0165
> 
> basic\_model = pm.Model()
> 
> with basic\_model:  
> nodes1 = pm.Normal(‘nodes1’, 0.05, 0.06)  
> nodes2 = pm.Normal(‘nodes2’, 1.0, 0.1)  
> nodes3 = pm.Normal(‘nodes3’, 50, 5)  
> mu=a.nodes1^2+b.nodes1.nodes2+c.nodes2^2+d.nodes1.nodes3+e.nodes1+f.nodes2.nodes3+g.nodes3^2+k.nodes2+m.nodes3+l  
> pm.Normal(‘observed’, mu, 0.05, observed=evals)  
> trace = pm.sample(1000, cores =1)  
> pm.traceplot(trace,varnames= [‘nodes1’,‘nodes2’,‘nodes3’])  
> k = pm.summary(trace).round(2)  
> pm.plot\_posterior (trace, varnames= [‘nodes1’,‘nodes2’,‘nodes3’])  
> tracedf1 = pm.trace\_to\_dataframe (trace, varnames = [‘nodes1’,‘nodes2’,‘nodes3’])  
> sns.pairplot(tracedf1 )  
> print (k)  
> plt.show()

in this my code just plot correlations of nodes1, nodes2 and nodes3 can’t plot with mu. And I also want to calculate correlation values between each pair between nodes1,nodes2 nodes3 and mu.

Having a problem I don’t understand in my code that is using the this code

> pm.Normal(‘observed’, mu, 0.05, observed=evals)

but I want to use the multivariate normal distribution so What things I need to find to I can apply the multivariate normal distribution to my problem. Because our prior distribution is Uniform distribution when I use it for **pm.Normal (‘observed’,mu,0.05, observed=evals)** the posterior distribution shape isn’t **Bell shape**.

please help me.

thank you
