3D图:x轴上的平滑图

问题描述 投票:2回答:2

我有一个3D多边形图,并且希望在y轴上平滑该图(即,我希望它看起来像是“表面图的切片”)。

考虑此MWE(摘自here:]

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
from scipy.stats import norm

fig = plt.figure()
ax = fig.gca(projection='3d')

xs = np.arange(-10, 10, 2)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]

for z in zs:
    ys = np.random.rand(len(xs))
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors=[mcolors.to_rgba('r', alpha=0.6),
                                         mcolors.to_rgba('g', alpha=0.6), 
                                         mcolors.to_rgba('b', alpha=0.6), 
                                         mcolors.to_rgba('y', alpha=0.6)])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel('X')
ax.set_xlim3d(-10, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()

现在,我想用正态分布替换这四个图(以理想地形成连续线)。

我在这里创建了发行​​版:

def get_xs(lwr_bound = -4, upr_bound = 4, n = 80):
    """ generates the x space betwee lwr_bound and upr_bound so that it has n intermediary steps """
    xs = np.arange(lwr_bound, upr_bound, (upr_bound - lwr_bound) / n) # x space -- number of points on l/r dimension
    return(xs)

xs = get_xs()

dists = [1, 2, 3, 4]

def get_distribution_params(list_):
    """ generates the distribution parameters (mu and sigma) for len(list_) distributions"""
    mus = []
    sigmas = []
    for i in range(len(dists)):
        mus.append(round((i + 1) + 0.1 * np.random.randint(0,10), 3))
        sigmas.append(round((i + 1) * .01 * np.random.randint(0,10), 3))
    return mus, sigmas

mus, sigmas = get_distribution_params(dists)

def get_distributions(list_, xs, mus, sigmas):
    """ generates len(list_) normal distributions, with different mu and sigma values """
    distributions = [] # distributions

    for i in range(len(list_)):
        x_ = xs
        z_ = norm.pdf(xs, loc = mus[i], scale = sigmas[0])
        distributions.append(list(zip(x_, z_)))
        #print(x_[60], z_[60])

    return distributions

distributions = get_distributions(list_ = dists, xs = xs, mus = mus, sigmas = sigmas)

但是将它们添加到代码中(poly = PolyCollection(distributions, ...)ax.add_collection3d(poly, zs=distributions, zdir='z')会抛出ValueErrorValueError: input operand has more dimensions than allowed by the axis remapping),我无法解析。

python python-3.x matplotlib z-axis
2个回答
0
投票

使用ax.add_collection3d(poly, zs=dists, zdir='z')而不是ax.add_collection3d(poly, zs=distributions, zdir='z')应该可以解决此问题。


另外,您可能想要替换

def get_xs(lwr_bound = -4, upr_bound = 4, n = 80):
    """ generates the x space betwee lwr_bound and upr_bound so that it has n intermediary steps """
    xs = np.arange(lwr_bound, upr_bound, (upr_bound - lwr_bound) / n) # x space -- number of points on l/r dimension
    return(xs)

xs = get_xs()

作者

xs = np.linspace(-4, 4, 80)

此外,我相信scale = sigmas[0]实际上应该是该行中的scale = sigmas[i]

z_ = norm.pdf(xs, loc = mus[i], scale = sigmas[0])

[最后,我相信您应该适当地调整xlimylimzlim,因为您交换了绘图的yz尺寸并在与参考代码进行比较时更改了比例尺。] >


0
投票

错误是由于将distributions传递到zs而引起的。 zs期望当verts中的PolyCollection具有形状MxNx2

© www.soinside.com 2019 - 2024. All rights reserved.