3.7 立体3D图
3.7.1配置参数
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib inline
get和set函数:在2d图中使用的函数在3d中基本上都要在前面加上set_。get相关的表示获取,set相关的表示设置
- set_xlabel() # 设置标签
- set_xticks() # 设置刻度
- a3.view_init(-10,60) # 3d图形视图的方向
- a3.get_default_bbox_extra_artists() # 获取默认图的参数
- a3.set_axis_on() # 显示坐标轴
- a3.set_axis_off() # 不显示坐标轴
三维散点图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib inline
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
z = np.random.normal(0, 1, 100)
fig = plt.figure()
ax = Axes3D(fig)
# ax = plt.subplot(projection = '3d')
ax.scatter3D(x,y,z)
三维线型图
fig = plt.figure(figsize = (9,6)) # 创建画布
a3 = Axes3D(fig) # 创建3d画布
x = np.linspace(0,60,1000)
y = np.sin(x)
z = np.cos(x)
a3.plot(x,y,z,linestyle = '-.',marker = '*',markersize = 5)
三维柱状图
fig = plt.figure(figsize=(12,9))
a3 = Axes3D(fig)
# 12个月的数据,横坐标X表示12月
# 每个月柱状图Y表示(车间) Z表示高度,产量
for i in np.arange(1,5): # 这个表示春夏秋冬四个季度,也就是x轴方向的刻度
a3.bar(
left = np.arange(1,7), # y轴方向的刻度
height = np.random.randint(5,30,size = 6), # 随机产生z轴方向的值,每次产生6个,产生4次
zs = i, # 将每次产生的i放在对应的位置
zdir = 'x' # 指定正方向为x轴的方向
)
a3.set_xticks(ticks=[1,2,3,4]) # 原生刻度
a3.set_xticklabels([*'春夏秋冬'],fontproperties = 'KaiTi',fontsize = 25) # 目标刻度
a3.set_xlabel('季度',color = 'red',fontproperties = 'KaiTi',fontsize = 25)
a3.set_ylabel('车间',color = 'red',fontproperties = 'FangSong',fontsize = 25)
a3.set_zlabel('产量',color = 'green',fontproperties = 'KaiTi',fontsize = 25)
我们可以看作是多个条形图的循环
三维曲面图
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
a3 = Axes3D(fig)
x = np.linspace(-2,2,100)
y = np.linspace(-2,2,100)
X,Y = np.meshgrid(x,y) # 绘制网格
Z = np.cos(np.sqrt(X**2 + Y**2))
a3.plot_surface(X,Y,Z,cmap = plt.cm.gist_heat)
三维等高线图
contour和contourf两者显示等高线的方式不一样
fig = plt.figure(figsize=(9,6)) # 画布的大小
a3 = Axes3D(fig)
a3.plot_surface(X,Y,Z,alpha = 0.5) # alpha透明度
a3.contour(X,Y,Z) # zdir指定等高线的投影方向,offset偏移量
a3.contourf(X,Y,Z,zdir = 'y',offset = 30) # X,Y,Z等高线的绘制,zdir登高先的投影位置,offset等高线的偏移量
3.8极坐标图(玫瑰图)
想要将直角坐标改成极坐标,只需在子图中添加polor=True这个属性即可。
- 子图
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(0,2*np.pi,np.pi/4)
y = np.random.randint(1,13,size = 8)
fig=plt.figure(figsize=(12,6)) # 创建画布
a1 = fig.add_subplot(1,2,1) # 添加子图
a1.bar(x,y,width = 0.6,color = np.random.rand(8,3))
a2 = fig.add_subplot(1,2,2,polar = True) # 添加子图
a2.bar(x,y,color = np.random.rand(8,3))
- 混合图(折线图)
- 玫瑰散点图
ax = plt.subplot(1,1,1,polar = True)
x = np.random.rand(150)*2*np.pi
y = np.random.rand(150)*2
ax.scatter(x,y,
c = x,
s = np.random.randint(100,200,size = 150),
cmap = 'hsv',
alpha = 0.75
)
3.9动图gif图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
%matplotlib inline
# %matplotlib notebook # 生成动图
fig = plt.figure() # 创建画布
x = np.linspace(-np.pi,np.pi,100) # x坐标
y = np.sin(x) # y坐标
plt.xticks([-np.pi,-0.5*np.pi,0,0.5*np.pi,np.pi]) # 指定刻度
plt.plot(x,y)
line= plt.plot(x[0],y[0],'ro')[0] # 起点
def func(i):
line.set_data(x[i],y[i]) # 重新设置这个起点
return line # 返回这个图
ani = animation.FuncAnimation(
fig, # 画布
func, # 动图函数
frames = np.arange(0,100), # 要给函数传递的数据源
interval=100 # 动图的帧数,时间为ms
)
ani.save('./sin.gif',writer = 'pillow',fps = 10) # 保存动图:保存路径,操作保存的对象,保存保存的分辨率
plt.show() # 显示动图