【问题标题】:3D plot: smooth plot on x axis3D 绘图:x 轴上的平滑绘图
【发布时间】:2020-02-02 08:25:20
【问题描述】:

我有一个 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') 会抛出 ValueError (ValueError: input operand has more dimensions than allowed by the axis remapping) 我无法解决。

【问题讨论】:

  • 请添加完整的非工作示例,其中包含导入、缺少规范和主题 = dists?

标签: python python-3.x matplotlib z-axis


【解决方案1】:

错误是由将distributions 传递给zs 引起的,其中zs 预计当PolyCollection 中的verts 具有形状MxNx2 传递给zs 的对象具有形状M。所以当它达到这个检查时

cpdef ndarray broadcast_to(ndarray array, shape):
    # ...
    if array.ndim < len(shape):
        raise ValueError(
            'input operand has more dimensions than allowed by the axis '
            'remapping')
    # ...

在底层 numpy 代码中,它失败了。我相信这是因为预期的维度数 (array.ndim) 小于 zs (len(shape)) 的维度数。它期待一个形状为(4,) 的数组,但接收到一个形状为(4, 80, 2) 的数组。

可以通过使用正确形状的数组来解决此错误 - 例如zs 来自原始示例或 dists 来自您的代码。使用zs=dists 并将xyz 的轴限制调整为[0,5] 给出

这看起来有点奇怪,原因有两个:

  1. z_ = norm.pdf(xs, loc = mus[i], scale = sigmas[0]) 中有一个错字,它使所有分布都具有相同的 sigma,它应该是 z_ = norm.pdf(xs, loc = mus[i], scale = sigmas[i])
  2. 观察几何:分布以正xz 平面为基础,这也是我们正在观察的平面。

通过ax.view_init 更改查看几何图形将产生更清晰的图:


编辑

这是生成所示图的完整代码,

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

np.random.seed(8)
def get_xs(lwr_bound = -4, upr_bound = 4, n = 80):
    return np.arange(lwr_bound, upr_bound, (upr_bound - lwr_bound) / n)

def get_distribution_params(list_):
    mus = [round((i+1) + 0.1 * np.random.randint(0,10), 3) for i in range(len(dists))]
    sigmas = [round((i+1) * .01 * np.random.randint(0,10), 3) for i in range(len(dists))]
    return mus, sigmas

def get_distributions(list_, xs, mus, sigmas):
    return [list(zip(xs, norm.pdf(xs, loc=mus[i], scale=sigmas[i] if sigmas[i] != 0.0 
            else 0.1))) for i in range(len(list_))]

dists = [1, 2, 3, 4]
xs = get_xs()
mus, sigmas = get_distribution_params(dists)
distributions = get_distributions(dists, xs, mus, sigmas)

fc = [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 = PolyCollection(distributions, fc=fc)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.add_collection3d(poly, zs=np.array(dists).astype(float), zdir='z')
ax.view_init(azim=115)
ax.set_zlim([0, 5])
ax.set_ylim([0, 5])
ax.set_xlim([0, 5])

我以您在问题中提供的代码为基础,但为了简洁起见并与通常的样式更加一致,做了一些修改。


注意   -   根据np.random.seed(),您给出的示例代码将失败,为了确保它正常工作,我在对norm.pdf 的调用中添加了一个检查,以确保规模是非零:scale = sigma[i] if sigma[i] != 0.0 else 0.1

【讨论】:

  • 您好威廉,非常感谢您的回复!我无法完全重现您的代码(即理解哪个 sn-ps 包含在哪里)而没有进一步的错误让我认为我弄错了。您能否澄清您建议的代码的外观? :) 非常感谢!
  • @Ivo 我很乐意,但我可能需要几天的时间来解决它....如果我在几天内没有更新我的答案,请随时联系我
  • 你有时间看这个吗? :)
  • @Ivo 我还没有 - 我有点忙,但我计划很快。感谢您对我的 ping 操作
  • @Ivo 我已经添加了我用来生成所示图的完整代码 - 希望这将有助于澄清事情
【解决方案2】:

使用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 尺寸并在与参考代码进行比较时更改了它的比例。

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多