Matplotlib plot_曲面透明度

2024-10-01 05:04:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一组指定z值的数据中绘制一个3D曲面。我得到了一些奇怪的透明艺术品,在那里我可以透过表面看到,即使我设置alpha=1.0。

打印时和保存到文件时(均为png和pdf格式)都会显示该工艺品: enter image description here

我试过改变线条宽度,把步幅从1改为10(在后一种情况下,由于分辨率太粗糙,表面不可见)。

问:我怎样才能摆脱这种透明性?

这是我的代码:

import sys
import numpy as np
import numpy.ma as ma
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

y_label = r'x'
x_label = r'y'
z_label = r'z'

x_scale = 2.0*np.pi
y_scale = 2.0*np.pi

y_numPoints = 250
x_numPoints = 250

def quasiCrystal(x, y):
    z = 0
    for i in range(0,5):
        z += np.sin(x * np.cos(float(i)*np.pi/5.0) +
                    y * np.sin(float(i)*np.pi/5.0))
    return z

x = np.linspace(-x_scale, x_scale, x_numPoints)
y = np.linspace(-y_scale, y_scale, y_numPoints)
X,Y = np.meshgrid(x,y)

Z = quasiCrystal(X, Y)


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

surf = ax.plot_surface( X, Y, Z,
                        rstride=5, cstride=5,
                        cmap='seismic',
                        alpha=1,
                        linewidth=0,
                        antialiased=True,
                        vmin=np.min(Z),
                        vmax=np.max(Z)
                      )

ax.set_zlim3d(np.min(Z), np.max(Z))

f.colorbar(surf, label=z_label)

ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_zlabel(z_label)

plt.show()

下面是我的实际数据的另一张图片,在这里更容易看到实物: enter image description here


Tags: 数据importalphanumpyasnppiplt
2条回答

Matplotlib不是一个真正的3D引擎。这是一个众所周知的问题,偶尔会出现一个与您类似的问题(请参见thisthis)。问题是同一件艺术品可能引发看起来不同的问题。我相信你就是这样。

在继续我的建议之前,让我先quote this information from the maplotlib website

My 3D plot doesn’t look right at certain viewing angles

This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look “physically correct.”

Unfortunately, while some work is being done to reduce the occurance of this artifact, it is currently an intractable problem, and can notbe fully solved until matplotlib supports 3D graphics rendering at itscore.

The problem occurs due to the reduction of 3D data down to 2D + z-order scalar. A single value represents the 3rd dimension for all parts of 3D objects in a collection. Therefore, when the bounding boxes of two collections intersect, it becomes possible for this artifact to occur. Furthermore, the intersection of two 3D objects (such as polygons or patches) can not be rendered properly in matplotlib’s 2D rendering engine.

This problem will likely not be solved until OpenGL support is added to all of the backends (patches are greatly welcomed). Until then, if you need complex 3D scenes, we recommend using MayaVi.

It seems that Mayavi has finally moved on to Python 3,这当然是一种可能性。如果要坚持使用matplotlib进行此类绘图,我的建议是使用rstride和cstride值来查看哪些值可以生成令您满意的绘图。

surf = ax.plot_surface( X, Y, Z,
                        rstride=5, cstride=5,
                        cmap='jet',
                        alpha=1,
                        linewidth=0,
                        antialiased=True,
                        vmin=0,
                        rstride=10,
                        cstride=10,
                        vmax=z_scale
                      )

另一种可能是尝试看看其他类型的3D绘图是否更好。检查plot_trisurfcontourcontourf。我知道这不太理想,但在过去我也设法做到了。

很抱歉没有更满意的答复。或许其他的用户对此有更好的解决方案。祝你好运。

我遇到了一些类似的问题,发现它们是抗锯齿工件,可以通过在plot_surface中设置antialiased=False来修复。

相关问题 更多 >