本文介绍了Matplotlib:使用plot_Surface在3D图形中显示Backgroundimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在3D图形的背景上显示.png图像的方法.我在此处使用

I'm looking for a way to show a .png image on the background of a 3D graph.I tried it with this Post here but even if i copy the exact code:

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()

I always get this Error:

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'

Is there a way I can solve this?

解决方案

It appears that there has been a change in matplotlib -- the third argument of plot_surface must be a 2D array. So wrap the constant with np.atleast_2d:

ax.plot_surface(X1, Y1, np.atleast_2d(-2.0), rstride=10, cstride=10, facecolors=arr)

Also note that arr.shape is (600, 512, 3)while X1.shape is (512, 600). This causes a shape mismatch which gives rise to an IndexError. To avoid this problem, swap the definitions of stepX and stepY:

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


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()

这篇关于Matplotlib:使用plot_Surface在3D图形中显示Backgroundimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:21