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. I looked up a bit on the internet and came across "imshow" but it doesn't seem to work on 3D plots. Does anyone have an idea on how to do so ? Thanks in advance :)
Asked
Active
Viewed 747 times
0
-
Have you seen this? https://stackoverflow.com/questions/13570287/image-overlay-in-3d-plot-using-python – Ed Smith Nov 16 '20 at 17:40
1 Answers
1
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). 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,
to get earth.

Ed Smith
- 12,716
- 2
- 43
- 55