Plotting a series of 2D plots projected in 3D in a perspectival way

旧巷老猫 提交于 2019-12-21 23:04:16

问题


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:

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.


回答1:


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 (%)')



来源:https://stackoverflow.com/questions/34099518/plotting-a-series-of-2d-plots-projected-in-3d-in-a-perspectival-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!