5

I'd like to plot a likelihood distribution, basically an NxT matrix, where each row represents a distribution on some variable in each timestep t (t=0...T), so I could visualize the trajectory which a Maximum Likelihood Estimation would yield.

I imagine several 2D plots, one in front of the other - something like this:

please ignore the axis labels

so far based on this I've tried:

def TrajectoryPlot(P):
    P=P[0:4]  
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    def cc(arg):
        return colorConverter.to_rgba(arg, alpha=0.6)
    xs = np.arange(0, len(P[0]))
    verts = []
    zs = [0.0, 1.0, 2.0, 3.0, 4.0]
    for i in range(len(P)):
        print(i)
        verts.append(list(zip(xs,  P[i])))    
    poly = PolyCollection(verts, facecolors=[cc('r'), cc('g'), cc('b'),
                                             cc('y')])
    poly.set_alpha(0.7)
    ax.add_collection3d(poly, zs=zs, zdir='y')
    ax.set_xlabel('X')
    ax.set_ylabel('Likelihood')
    ax.set_zlabel('Time')
    plt.show()

But this does not work yet.

ben0it8
  • 505
  • 1
  • 6
  • 10
  • Have you looked in the [matplotlib gallery](http://matplotlib.org/gallery.html)? Specifically, a combination of [this](http://matplotlib.org/examples/mplot3d/2dcollections3d_demo.html) and [this](http://matplotlib.org/examples/mplot3d/polys3d_demo.html) would fulfil your needs. – ali_m Dec 05 '15 at 01:00
  • honestly I'm not too skilled in matplotlib, so these are completely incomprehensible to me. as far as i can see there's no straightforward way to do it. – ben0it8 Dec 05 '15 at 01:07
  • What have you tried so far? If I were you, I would start out by making a single plot on normal 2D axes corresponding to one 'row' in your matrix. I'm willing to help you if you get stuck on something specific, but SO isn't a code-writing service. I couldn't give you a complete example even if I wanted to, since you haven't clearly explained your data structure or what the plot is supposed to represent. If matplotlib really is 'incomprehensible' to you then perhaps you should start out with [one of the tutorials](http://matplotlib.org/resources/index.html#tutorials). – ali_m Dec 05 '15 at 01:32

1 Answers1

8

The fill_between routine also returns a PolyCollection object, so you could use fill_between and add that using add_collection3d:

import matplotlib.pylab as pl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

x   = np.linspace(1,5,100)
y1  = np.ones(x.size)
y2  = np.ones(x.size)*2
y3  = np.ones(x.size)*3
z   = np.sin(x/2)

pl.figure()
ax = pl.subplot(projection='3d')
ax.plot(x, y1, z, color='r')
ax.plot(x, y2, z, color='g')
ax.plot(x, y3, z, color='b')

ax.add_collection3d(pl.fill_between(x, 0.95*z, 1.05*z, color='r', alpha=0.3), zs=1, zdir='y')
ax.add_collection3d(pl.fill_between(x, 0.90*z, 1.10*z, color='g', alpha=0.3), zs=2, zdir='y')
ax.add_collection3d(pl.fill_between(x, 0.85*z, 1.15*z, color='b', alpha=0.3), zs=3, zdir='y')

ax.set_xlabel('Day')
ax.set_zlabel('Resistance (%)')

enter image description here

Bart
  • 9,825
  • 5
  • 47
  • 73