Matplotlib 3D投影将线条扩大到更远的地方

时间:2018-08-06 10:54:59

标签: python matplotlib

我正在尝试模拟Tensorboards直方图,并提出以下内容:

enter image description here

现在,所有绘图线都具有相同的linewidth,但是,较远的绘图线会被更粗地绘制。我意识到matplotlib并不是真正的3d绘图框架,但是有什么可以做的吗?

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D     # noqa
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np


def make_fill_polygons(xs, ys, zs):
    v = []
    h = 0
    for k in range(0, len(xs) - 1):
        x = [xs[k], xs[k+1], xs[k+1], xs[k]]
        y = [ys[k], ys[k+1], ys[k+1], ys[k]]
        z = [zs[k], zs[k+1],       h,     h]
        v.append(list(zip(x, y, z)))
    poly3dCollection = Poly3DCollection(v)
    return poly3dCollection


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r, g, b = (199 / 255, 64 / 255, 24 / 255)

nbins = 100
n_hists = 10
n_data = 1000
unit = 1 / n_hists

for i in range(n_hists):
    X = np.random.normal(size=n_data)
    hist, edges = np.histogram(X, bins=nbins, density=True)
    xpos = edges[:-1]
    ypos = i * unit * np.ones_like(edges[:-1])
    zpos = hist

    percent = i / n_hists
    color = (r + percent * (1.0 - r), g + percent * (1.0 - g), g + percent * (1.0 - g))
    ax.plot(xpos, ypos, zpos, color='black', linewidth=2)
    collection = make_fill_polygons(xpos, ypos, zpos)
    collection.set_facecolor(color)
    collection.set_edgecolor(color)
    ax.add_collection3d(collection)
    ax.relim()
plt.show()

0 个答案:

没有答案