Python Matplotlib:使用plot\U曲面在三维图形中显示背景图像

Python Matplotlib:使用plot\U曲面在三维图形中显示背景图像,python,image,matplotlib,plot,png,Python,Image,Matplotlib,Plot,Png,我正在寻找一种在3D图形背景上显示.png图像的方法。 我在这里尝试过,但即使我复制了确切的代码: from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np from matplotlib._pn

我正在寻找一种在3D图形背景上显示.png图像的方法。 我在这里尝试过,但即使我复制了确切的代码:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
from matplotlib._png import read_png
from matplotlib.cbook import get_sample_data

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, .25)
Y = np.arange(-5, 5, .25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter,
                       linewidth=0, antialiased=True)

ax.set_zlim(-2.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fn = get_sample_data("./grace_hopper.png", asfileobj=False)
arr = read_png(fn)
# 10 is equal length of x and y axises of your surface
stepX, stepY = 10. / arr.shape[0], 10. / arr.shape[1]

X1 = np.arange(-5, 5, stepX)
Y1 = np.arange(-5, 5, stepY)
X1, Y1 = np.meshgrid(X1, Y1)
Z = 
# stride args allows to determine image quality 
# stride = 1 work slow
ax.plot_surface(X1, Y1, 2.0, rstride=1, cstride=1, facecolors=arr)

plt.show()
我总是会遇到这样的错误:

Traceback (most recent call last):
  File "c:\Users\XXX\ZeichnenFabrik\readTextFile.py", line 161, in <module>
    main()
  File "c:\Users\XXX\ZeichnenFabrik\readTextFile.py", line 157, in main
    plotGraph(nodedict,slines)
  File "c:\Users\XXX\ZeichnenFabrik\readTextFile.py", line 145, in plotGraph
    ax.plot_surface(X1, Y1, 0)
  File "C:\Program Files\Python36\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1609, in plot_surface
    if Z.ndim != 2:
AttributeError: 'int' object has no attribute 'ndim' 
回溯(最近一次呼叫最后一次):
文件“c:\Users\XXX\ZeichnenFabrik\readTextFile.py”,第161行,在
main()
文件“c:\Users\XXX\ZeichnenFabrik\readTextFile.py”,第157行,在main中
绘图仪(nodedict、sline)
plotGraph中第145行的文件“c:\Users\XXX\ZeichnenFabrik\readTextFile.py”
ax.绘图曲面(X1,Y1,0)
文件“C:\Program Files\Python36\lib\site packages\mpl\u toolkits\mplot3d\axes3d.py”,第1609行,在plot\u曲面中
如果Z.ndim!=2:
AttributeError:“int”对象没有属性“ndim”

有什么办法可以解决这个问题吗?

看来matplotlib中有一个变化--
plot\u surface
的第三个参数必须是2D数组。因此,用
np包装常量。至少\u 2d

ax.plot_surface(X1, Y1, np.atleast_2d(-2.0), rstride=10, cstride=10, facecolors=arr)
还要注意,
arr.shape
(600512,3)
X1.shape
(512600)
。这会导致形状不匹配,从而产生
索引器。要避免此问题,请交换
stepX
stepY
的定义:

height, width = arr.shape[:2]
stepX, stepY = 10.0/width, 10.0/height


发布您得到的错误的跟踪信息。这会更容易找到原因。这很有效!非常感谢你!不幸的是,我的形象被扭曲了。如何修复此问题?您可以恢复
rstride=1,cstride=1
,这样图像中的每个像素都会映射到
绘图曲面上的一个点。我在上面的例子中没有这样做,所以代码运行得更快。
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
from matplotlib._png import read_png
from matplotlib.cbook import get_sample_data

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

X = np.arange(-5, 5, .25)
Y = np.arange(-5, 5, .25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter,
                       linewidth=0, antialiased=True)

ax.set_zlim(-2.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fn = get_sample_data("./grace_hopper.png", asfileobj=False)
arr = read_png(fn)
height, width = arr.shape[:2]
# 10 is equal length of x and y axises of your surface
stepX, stepY = 10.0/width, 10.0/height

X1 = np.arange(-5, 5, stepX)
Y1 = np.arange(-5, 5, stepY)
X1, Y1 = np.meshgrid(X1, Y1)
ax.plot_surface(X1, Y1, np.atleast_2d(-2.0), rstride=10, cstride=10, facecolors=arr)

plt.show()