畫彩蛋
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from PIL import Image
import numpy as np
fig=plt.figure()
#projection='3d'的意思是繪製三維圖形,否則繪製的就是平面圖形,彩蛋就不會那麼立體了
ax=fig.gca(projection='3d')
u = np.linspace(0,2*np.pi,1000)
v = np.linspace(0,2*np.pi,1000)
x=10*np.outer(np.cos(u),np.sin(v))
y=10*np.outer(np.sin(u),np.sin(v))
z=6*np.outer(np.ones(np.size(u)),np.cos(v))
ax.plot_surface(x,y,z,cmap='rainbow')
#plt.cm.get_cmap(value))
plt.plot([-15,0,10],[0,0,0],[0,0,0],color='indigo',linestyle='--')
plt.plot([0,0,0],[-15,0,10],[0,0,0],color='indigo',linestyle='--')
plt.plot([0,0,0],[0,0,0],[-10,0,6],color='indigo',linestyle='--')
ax.quiver(0, 0, 6, 0, 0, 5, length=1, color='indigo',linestyle='--')
ax.quiver(0, 10, 0, 0, 5, 0, length=1, color='indigo',linestyle='--')
ax.quiver(10, 0, 0, 5, 0, 0, length=1, color='indigo',linestyle='--')
print(plt.cm.cmap_d.keys())
plt.show()
ims=[Image.open(filepath+"\\"+fn) for fn in listdir(filepath) if fn.endswith('.png')]
#columns的意思是一行包括幾個彩蛋,如果等於5,每行就是五個彩蛋,第六個彩蛋就會出現在下一行
result=Image.new(ims[0].mode,(width*rows,math.ceil(height*len(ims)/rows)))
for i,im in enumerate(ims):
row=math.floor(i/rows)
column=i%rows
result.paste(im,box=(column*width,row*height))
繪製3D輪廓
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()
繪製3D三角面片圖
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
n_radii = 8
n_angles = 36
# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
# Repeat all angles for each radius.
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
# Convert polar (radii, angles) coords to cartesian (x, y) coords.
# (0, 0) is manually added at this stage, so there will be no duplicate
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
# Compute z to make the pringle surface.
z = np.sin(-x*y)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
plt.show()