背景
需利用python进行3D可视化处理,用于分析python得到的数据的正确性。
知识学习
python高阶3D绘图---pyvista模块,mayavi模块,pyopengl模块,MoviePy模块基础使用-CSDN博客
python用于3D绘图的模块比较多,pyvista模块,mayavi模块,pyopengl模块,MoviePy模块matplotlib库。建议直接看官方教程比较快。工具很多,具体使用哪一种,根据各模块的特点选用。
如下官网链接
Mayavi: 3D scientific data visualization and plotting in Python — mayavi 4.8.3.dev0 文档
Matplotlib 文档_Matplotlib 中文网
Matplotlib — Visualization with Python (推荐看英文官网)
Examples — PyVista 0.43.1 documentation
PyOpenGL -- The Python OpenGL Binding (sourceforge.net)
MoviePy中文手册 — moviepy-cn 文档
应用
代码如下:
这个项目是因为,已经将得到的数据存在xlsx中,所以增加了读数据的操作,直接调用python的数据也一样,主要用到如下代码用于3d 散点图的显示
# 显示图形fig = plt.figure(figsize=(10, 8)) # 设置图形大小ax = fig.add_subplot(111, projection='3d')ax.scatter(Oab_rows_x, Oab_rows_y, Oab_rows_z, label='O set', s=1, alpha=0.8)ax.scatter(Pxb_rows_x, Pxb_rows_y, Pxb_rows_z, color='red', label='x set', s=1, alpha=0.8)ax.scatter(Pyb_rows_x, Pyb_rows_y, Pyb_rows_z, color='black', label='y set', s=1, alpha=0.8)# 添加图例ax.legend()plt.show()
官网中还有很多图例,如下截图
怎么设置图例,出图漂亮
一个应用代码如下: (仅做学习记录,画图看官网)
import numpy as np
import math
import pandas as pdimport matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as pltimport openxlsx# 主函数
def main(excel_file, sheet_name):# 读取Excel文件df = pd.read_excel(excel_file, sheet_name=sheet_name)result_rows = []Oab_rows_x = []Oab_rows_y = []Oab_rows_z = []Pxb_rows_x = []Pxb_rows_y = []Pxb_rows_z = []Pyb_rows_x = []Pyb_rows_y = []Pyb_rows_z = []# 遍历DataFrame的每一行for index, row in df.iterrows():Oab = np.array([row['Oab_x'], row['Oab_y'], row['Oab_z']])Pxb = np.array([row['Pxb_x'], row['Pxb_y'], row['Pxb_z']])Pyb = np.array([row['Pyb_x'], row['Pyb_y'], row['Pyb_z']])Oab_row_x = Oab[0]Oab_rows_x.append(Oab_row_x)Oab_row_y = Oab[1]Oab_rows_y.append(Oab_row_y)Oab_row_z = Oab[2]Oab_rows_z.append(Oab_row_z)Pxb_row_x = Pxb[0]Pxb_rows_x.append(Pxb_row_x)Pxb_row_y = Pxb[1]Pxb_rows_y.append(Pxb_row_y)Pxb_row_z = Pxb[2]Pxb_rows_z.append(Pxb_row_z)Pyb_row_x = Pyb[0]Pyb_rows_x.append(Pyb_row_x)Pyb_row_y = Pyb[1]Pyb_rows_y.append(Pyb_row_y)Pyb_row_z = Pyb[2]Pyb_rows_z.append(Pyb_row_z)# 显示图形fig = plt.figure(figsize=(10, 8)) # 设置图形大小ax = fig.add_subplot(111, projection='3d')ax.scatter(Oab_rows_x, Oab_rows_y, Oab_rows_z, label='O set', s=1, alpha=0.8)ax.scatter(Pxb_rows_x, Pxb_rows_y, Pxb_rows_z, color='red', label='x set', s=1, alpha=0.8)ax.scatter(Pyb_rows_x, Pyb_rows_y, Pyb_rows_z, color='black', label='y set', s=1, alpha=0.8)# 添加图例ax.legend()plt.show()# 调用主函数
if __name__ == "__main__":excel_file = 'C:\\Users\Desktop\output.xlsx' # 假设Excel文件名是data.xlsxsheet_name = 'Sheet 1' # 假设数据在第一个工作表上main(excel_file, sheet_name)
其中如下代码是因为报错,可参照链接解决。
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
matplotlib:报错:ImportError: Cannot load backend ‘TkAgg‘ which requires the ‘tk‘ interactive 的处理记录_importerror: cannot load backend 'tkagg' which req-CSDN博客
拓展:matplotlib 官方网站
按数据分类:
Pairwise data#
成对、表格、 和函数数据。\((x, y)\)\((var\_0, \cdots, var\_n)\)\(f(x)=y\)
统计类数据
数据集中至少一个变量的分布图。其中一些方法还计算分布。
网格化数据
不规则的网格化数据
3D和立体数据
用户指导
目录如下
Quick start guide#
- A simple example
- Parts of a Figure
- Types of inputs to plotting functions
- Coding styles
- Styling Artists
- Labelling plots
- Axes labels and text
- Using mathematical expressions in text
- Annotations
- Legends
- Axis scales and ticks
- Color mapped data
- Working with multiple Figures and Axes
- More reading
上图是figure的组成元素。要是绘制简单的图形,直接参照Quick start guide官方代码。也可以直接看教程。
教程
Introductory# 初级
-
Quick start guide
-
Customizing Matplotlib with style sheets and rcParams 自定义属性及参数设置
-
Animations using Matplotlib 动画
Intermediate 中级
-
Legend guide 图例指南
-
Styling with cycler 演示自定义属性周期设置,以控制多线绘图的颜色和其他样式属性。
-
Constrained layout guide 约束布局
-
Tight layout guide 紧凑布局
-
Arranging multiple Axes in a Figure 多坐标
-
Autoscaling Axis 可缩放
-
origin and extent in imshow
Advanced 高级
-
Faster rendering by using blitting 动画
-
Path Tutorial 轨迹
-
Path effects guide 轨迹效果
-
Transformations Tutorial 官网说应用较少
Colors 颜色设置
See Colors.
Text 文本设置
See Text.
Toolkits 工具箱子
See User Toolkits.
- The axisartist toolkit 自定义轴类
- The axes_grid1 toolkit 网格图工具
- The mplot3d toolkit 3D图工具
Contents
-
The mplot3d toolkit
-
Line plots
-
Scatter plots
-
Wireframe plots
-
Surface plots
-
Tri-Surface plots
-
Contour plots
-
Filled contour plots
-
Polygon plots
-
Bar plots
-
Quiver
-
2D plots in 3D
-
Text
-