文章传送门
| Python 数据可视化 | 
|---|
| matplotlib之绘制常用图形:折线图、柱状图(条形图)、饼图和直方图 | 
| matplotlib之设置坐标:添加坐标轴名字、设置坐标范围、设置主次刻度、坐标轴文字旋转并标出坐标值 | 
| matplotlib之增加图形内容:设置图例、设置中文标题、设置网格效果 | 
| matplotlib之设置子图:绘制子图、子图共享x轴坐标 | 
| matplotlib之绘制高级图形:散点图、热力图、等值线图、极坐标图 | 
| matplotlib之绘制三维图形:三维散点图、三维柱状图、三维曲面图 | 
目录
- 简述 / 前言
- 1. 三维散点图
- 2. 三维柱状图
- 3. 三维曲面图
简述 / 前言
前面介绍了一些常用技巧、常见的图形(折线图、柱状图(条形图)、饼图和直方图)和高级的二维图形(散点图、热力图、等值线图),这篇分享一下如何绘制三维图形:三维散点图、三维柱状图、三维曲线图。
三维图形需要用到 mpl_toolkits.mplot3d 中的 Axes3D,也可以用前面介绍的二维方法创建三维图像,前提是要将子图的坐标轴改为三维坐标轴。
1. 三维散点图
关键方法:Axes3D().scatter(),注意需要创建 figure 对象。
一般用法:.scatter(x, y, z),各参数含义如下:
| 参数 | 含义 | 
|---|---|
| x | x轴的数据 | 
| y | y轴的数据 | 
| z | z轴的数据 | 
示例:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 三门课程的成绩
x = [100, 98, 79, 68, 85]
y = [95, 99, 80, 60, 90]
z = [93, 90, 85, 70, 88]
# 绘制三维散点图
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z)
# 添加坐标轴
plt.rcParams['font.sans-serif'] = ['FangSong']  # 设置中文
ax.set_xlabel('课程1')
ax.set_ylabel('课程2')
ax.set_zlabel('课程3')
plt.show()
输出:
 
整体结构和二维散点图差不多,具体的用法也很相似,只是多了一个z轴而已
2. 三维柱状图
关键参数:projection='3d',指定子图的坐标轴为三维坐标轴。
关键语句:matplotlib.pyplot.bar3d(x, y, z, dx, dy, dz);
一般会这么写:matplotlib.pyplot.bar3d(x, y, z=0, dx=1, dy=1, dz, zsort='average', shade=True, alpha=alpha, color=color, edgecolor=edgecolor, lw=lw, linestyle=linestyle),各参数含义如下:
| 属性 | 含义 | 
|---|---|
| x, y, z | 定位点的坐标(输入的数据类型类似于数组)【z一般取0,表示柱状图的底都在平面 X O Y XOY XOY上】 | 
| dx, dy | 柱状图底面的长和宽【一般都取1】 | 
| dz | 柱状图的高度 | 
| zsort | z轴的排序方案 | 
| shade | 是否加上阴影(默认为: True)【如果为真,这会使条形的暗边变暗(相对于图的光源)。】 | 
| alpha | 透明度,取值范围:[0, 1],值越小越透明 | 
| color | 每个柱状图的颜色【颜色只能取一种】 | 
| edgecolor | 边缘的颜色【颜色可以取几种,但是并不是每个柱状图的边缘颜色都不一样,只是在移动查看的时候,颜色会不断转换,建议只取一种颜色,或者不设置边缘颜色】 | 
| lw | 边缘线的宽度 | 
| linestyle | 边缘线的类型 | 
示例:
import matplotlib.pyplot as plt
x = [1, 6, 3, 4, 5, 6, 3]     # x轴刻度
y = [1, 3, 3, 4, 5, 1, 2]       # y轴刻度
dz = [12, 20, 25, 10, 18, 5, 8]   # z轴刻度
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title('三维柱状图')
ax.bar3d(x, y, z=0, dx=1, dy=1, dz=dz, shade=True, color='g', edgecolor='grey', lw=3, linestyle='--', alpha=0.5)
plt.rcParams['font.sans-serif'] = ['FangSong']  # 设置中文
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
plt.show()
输出:
 
3. 三维曲面图
关键参数:projection='3d',指定子图的坐标轴为三维坐标轴。
关键方法:.plot_surface()
一般会这么写:.plot_surface(X, Y, Z, rstride=rstride, cstride=cstride, cmap=cmap),各参数含义如下:
| 参数 | 含义 | 
|---|---|
| X | X轴的位置序列 | 
| Y | Y轴的位置序列 | 
| Z | 要绘制的曲线的函数 | 
| rstride | X轴方向上的条纹间隔 | 
| cstride | Y轴方向上的条纹间隔 | 
| cmap | 指定填充风格,有内置颜色,也可以自定义颜色,内置的风格有: Accent,Accent_r,Blues,Blues_r,BrBG,BrBG_r,BuGn,BuGn_r,BuPu,BuPu_r,CMRmap,CMRmap_r,Dark2,Dark2_r,GnBu,GnBu_r,Greens,Greens_r,Greys,Greys_r,OrRd,OrRd_r,Oranges,Oranges_r,PRGn,PRGn_r,Paired,Paired_r,Pastel1,Pastel1_r,Pastel2,Pastel2_r,PiYG,PiYG_r,PuBu,PuBuGn,PuBuGn_r,PuBu_r,PuOr,PuOr_r,PuRd,PuRd_r,Purples,Purples_r,RdBu,RdBu_r,RdGy,RdGy_r,RdPu,RdPu_r,RdYlBu,RdYlBu_r,RdYlGn,RdYlGn_r,Reds,Reds_r,Set1,Set1_r,Set2,Set2_r,Set3,Set3_r,Spectral,Spectral_r,Wistia,Wistia_r,YlGn,YlGnBu,YlGnBu_r,YlGn_r,YlOrBr,YlOrBr_r,YlOrRd,YlOrRd_r,afmhot,afmhot_r,autumn,autumn_r,binary,binary_r,bone,bone_r,brg,brg_r,bwr,bwr_r,cividis,cividis_r,cool,cool_r,coolwarm,coolwarm_r,copper,copper_r,cubehelix,cubehelix_r,flag,flag_r,gist_earth,gist_earth_r,gist_gray,gist_gray_r,gist_heat,gist_heat_r,gist_ncar,gist_ncar_r,gist_rainbow,gist_rainbow_r,gist_stern,gist_stern_r,gist_yarg,gist_yarg_r,gnuplot,gnuplot2,gnuplot2_r,gnuplot_r,gray,gray_r,hot,hot_r,hsv,hsv_r,inferno,inferno_r,jet,jet_r,magma,magma_r,nipy_spectral,nipy_spectral_r,ocean,ocean_r,pink,pink_r,plasma,plasma_r,prism,prism_r,rainbow,rainbow_r,seismic,seismic_r,spring,spring_r,summer,summer_r,tab10,tab10_r,tab20,tab20_r,tab20b,tab20b_r,tab20c,tab20c_r,terrain,terrain_r,turbo,turbo_r,twilight,twilight_r,twilight_shifted,twilight_shifted_r,viridis,viridis_r,winter,winter_r | 
示例:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-11, 11, 0.1)
X, Y = np.meshgrid(x, x)
Z = X ** 2 + Y ** 2  # 定义待绘制的曲线
# 绘制三维曲线
ax.plot_surface(X, Y, Z, rstride=10, cstride=20, cmap='summer_r')
ax.set_xlabel('x轴', fontdict={'fontname': 'FangSong', 'fontsize': 16})
ax.set_ylabel('y轴', fontdict={'fontname': 'FangSong', 'fontsize': 16})
ax.set_zlabel('z轴', fontdict={'fontname': 'FangSong', 'fontsize': 16})
plt.show()
输出:
 



















