如果有喜欢的模板,可以直接套数据到官方提供的模板。
https://plotly.com/python/
3D
#创建一个子图,带3D的图纸
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
plt.show()
#生成一个,Z轴线
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)
plt.show()
#生成一个3D的图,散点图
np.random.seed(1)
def randrange(n,vmin,vmax):
return (vmax-vmin)*np.random.rand(n)+vmin
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
n = 100
for c,m,zlow,zhigh in [('r','o',-50,-25),('b','x','-30','-5')]:
xs = randrange(n,23,32)
ys = randrange(n,0,100)
zs = randrange(n,int(zlow),int(zhigh))
ax.scatter(xs,ys,zs,c=c,marker=m)
plt.show()
生成3D图,矩形图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
cs = [c]*len(xs)
ax.bar(xs,ys,zs = z,zdir='y',color = cs,alpha = 0.5)
plt.show()
生成3D 等高线图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
'''让下面的都在一个框框里面'''
ax = Axes3D(fig)
fig.add_axes(ax)
'''绘制3D空间(坐标轴)'''
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
'''把x,y绘制对应到底面的面上去'''
R = np.sqrt(X ** 2 + Y ** 2 + np.exp(np.pi))
Z = np.tanh(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
'''绘制3D cmap又一种方法'''
ax.contourf(X, Y, Z, zdir='z', offset=0.99987, cmap='summer')
'''顺便在某平面画个等高线 zdir是决定从哪个方向压下去'''
plt.show()