简体   繁体   English

在 matplotlib 3D 绘图中使用图像作为背景

[英]Use an image as a background in a matplotlib 3D plot

I'm trying to emulate the earth going around the sun using matplotlib with Axes3D, and I thought that it could be great if I can a Stary nightsky as background.我正在尝试使用带有 Axes3D 的 matplotlib 来模拟地球围绕太阳转,我认为如果我可以将星空作为背景会很棒。 I looked up a bit on the internet and came across "imshow" but it doesn't seem to work on 3D plots.我在互联网上查了一下,遇到了“imshow”,但它似乎不适用于 3D 绘图。 Does anyone have an idea on how to do so ?有没有人知道如何做到这一点? Thanks in advance :)提前致谢 :)

You could try to put images in the background in all 6 directions following the approach here ( Image overlay in 3d plot using python ) but another option is to combine setting a black background ( Change 3D background to black in matplotlib ) with some white points (like stars, you can also vary size s and alpha at random to improve the effect).您可以尝试按照此处的方法将图像放在所有 6 个方向的背景中( 使用 python 在 3d 图中叠加图像),但另一种选择是将设置黑色背景( 在 matplotlib 中将 3D 背景更改为黑色)与一些白点(像星星一样,您也可以随机改变大小s和 alpha 以改善效果)。 The code for this is,这个代码是,

import PIL
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

bm = PIL.Image.open('bluemarble.jpg')
bm = np.array(bm.resize([int(d/5) for d in bm.size]))/256.
lons = np.linspace(-180, 180, bm.shape[1]) * np.pi/180 
lats = np.linspace(-90, 90, bm.shape[0])[::-1] * np.pi/180 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.outer(np.cos(lons), np.cos(lats)).T
y = np.outer(np.sin(lons), np.cos(lats)).T
z = np.outer(np.ones(np.size(lons)), np.sin(lats)).T
ax.plot_surface(x, y, z, rstride=4, cstride=4, facecolors = bm)

#Add some stars
x, y, z = 10*(np.random.rand(3,100)-0.5)
ax.scatter(x, y, z, s=0.1, c='w')

#Set backgroudn to black
fig.set_facecolor('black')
ax.set_facecolor('black') 
ax.grid(False) 
ax.w_xaxis.pane.fill = False
ax.w_yaxis.pane.fill = False
ax.w_zaxis.pane.fill = False

plt.show()

which looks as follows,如下所示,

在此处输入图片说明

I copied the following example ( Creating a rotatable 3D earth ) with this image,我用这张图片复制了以下示例( 创建可旋转的 3D 地球),

在此处输入图片说明

to get earth.得到地球。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM