【Python数据可视化】matplotlib之绘制三维图形:三维散点图、三维柱状图、三维曲面图

文章传送门

Python 数据可视化
matplotlib之绘制常用图形:折线图、柱状图(条形图)、饼图和直方图
matplotlib之设置坐标:添加坐标轴名字、设置坐标范围、设置主次刻度、坐标轴文字旋转并标出坐标值
matplotlib之增加图形内容:设置图例、设置中文标题、设置网格效果
matplotlib之设置子图:绘制子图、子图共享x轴坐标
matplotlib之绘制高级图形:散点图、热力图、等值线图、极坐标图
matplotlib之绘制三维图形:三维散点图、三维柱状图、三维曲面图

目录

  • [简述 / 前言](#简述 / 前言)
  • [1. 三维散点图](#1. 三维散点图)
  • [2. 三维柱状图](#2. 三维柱状图)
  • [3. 三维曲面图](#3. 三维曲面图)

简述 / 前言

前面介绍了一些常用技巧、常见的图形(折线图、柱状图(条形图)、饼图和直方图)和高级的二维图形(散点图、热力图、等值线图),这篇分享一下如何绘制三维图形:三维散点图、三维柱状图、三维曲线图。

三维图形需要用到 mpl_toolkits.mplot3d 中的 Axes3D,也可以用前面介绍的二维方法创建三维图像,前提是要将子图的坐标轴改为三维坐标轴。


1. 三维散点图

关键方法:Axes3D().scatter(),注意需要创建 figure 对象。

一般用法:.scatter(x, y, z),各参数含义如下:

参数 含义
x x轴的数据
y y轴的数据
z z轴的数据

示例:

py 复制代码
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 边缘线的类型

示例:

py 复制代码
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

示例:

py 复制代码
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()

输出:

相关推荐
Hgfdsaqwr8 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
开发者小天9 小时前
python中For Loop的用法
java·服务器·python
老百姓懂点AI9 小时前
[RAG实战] 向量数据库选型与优化:智能体来了(西南总部)AI agent指挥官的长短期记忆架构设计
python
喵手11 小时前
Python爬虫零基础入门【第九章:实战项目教学·第15节】搜索页采集:关键词队列 + 结果去重 + 反爬友好策略!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·搜索页采集·关键词队列
Suchadar11 小时前
if判断语句——Python
开发语言·python
ʚB҉L҉A҉C҉K҉.҉基҉德҉^҉大11 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
喵手12 小时前
Python爬虫零基础入门【第九章:实战项目教学·第14节】表格型页面采集:多列、多行、跨页(通用表格解析)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·表格型页面采集·通用表格解析
0思必得012 小时前
[Web自动化] 爬虫之API请求
前端·爬虫·python·selenium·自动化
莫问前路漫漫12 小时前
WinMerge v2.16.41 中文绿色版深度解析:文件对比与合并的全能工具
java·开发语言·python·jdk·ai编程
木头左12 小时前
Backtrader框架下的指数期权备兑策略资金管理实现与风险控制
python