【绘图】3D点图 及绘图关系matplotlib中plt系列

1. 报错及解决方案:

报错:
在这里插入图片描述
首先需要知道,这是由于没有使用绘制3D图,但没有加入3D库引起的。
所以需要添加如下的库:

from mpl_toolkits.mplot3d import Axes3D

触发语句:

ax = fig.add_subplot(111, projection='3d')

2. matplotlib中plt绘图关系描述

然后发现其实自己也没有搞明白绘图当中的一些关系,所以干脆顺便学习了一下,分享在这里:
库matplotlib中的plt相关绘图

2.1 plt.figure()

作用:绘制一张图
函数
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
参数说明

  • num:图像编号或名称,数字为编号 ,字符串为名称
  • figsize:指定figure的宽和高,单位为英寸
  • dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
  • facecolor:背景颜色
  • edgecolor:边框颜色
  • frameon:是否显示边框

一般用法

import matplotlib.pyplot as plt
fig=plt.figure()
plt.show()

2.2 plt.subplot()

作用:创建单个子图
函数:subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)
参数说明

  • nrows:子图的行数
  • ncols:子图的列数
  • sharex:所有子图使用相同的X轴刻度(调节xlim将会影响所有的subplot)
  • sharey:所有子图使用相同的Y轴刻度(调节ylim将会影响所有subplot)
  • subplot_kw:用于创建个子图的关键字字典
  • **fig_kw:创建figure时的其他关键字。

一般用法

# 绘制一行两个子图,一个是y=2x,一个是y=x
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10)
y = 2 * x
plt.subplot(121)
plt.plot(x, y)
plt.subplot(122)
plt.plot(x, x)
plt.show()

2.3 plt.subplots()

作用:创建多个子图
说明:subplots参数与subplots类似。区别在于每条subplot命令只会创建一个子图,而一条subplots就可以将所有子图创建好。
一般用法

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10)
y = 2 * x
#划分子图
fig,axes=plt.subplots(1,2)
ax1=axes[0,0]# 左图
ax2=axes[0,1]# 右图
#作图1
ax1.plot(x, y)
#作图2
ax2.plot(x, x)
plt.show()

2.4 add_subplots

作用:给figure新添加子图,与subpot类似,一般可以配合生成3D数据。
用法

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # 为了3D绘图
x = np.arange(0, 10)
y = 2 * x
z = 2 * y 
fig = plt.figure()
ax =  fig.add_subplot(111, projection='3d') # 这里需要加参数projection='3d'
ax.scatter(x, y, z, marker='+', color='blue', label='Real')
plt.show()

2.5 add_axes

作用:新添加子区域
函数: fig.add_axes([left, bottom, width, height])
参数说明:left 与bottom表示左边以及下边的起始位置,width和height表示宽高占原fig的比例。
用法

import matplotlib.pyplot as plt
#创建figure
fig = plt.figure()
x = np.arange(0, 10)
y = 2 * x
#新建区域ax1
#figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# 获得绘制的句柄
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, 'r')
ax1.set_title('area1')
 
#新增区域ax2,嵌套在ax1内
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# 获得绘制的句柄
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(x,y, 'b')
ax2.set_title('area2')
plt.show()

3. 图层叠加

参数:zorder
或者使用matplotlib的set_zorder这个属性.
但是发现如果使用Axes3D,其竟然是失效的。。。甚是无语啊,所以看到一种方法是放弃使用该方法使用mayavi库。

参考

https://blog.csdn.net/C_chuxin/article/details/83994457
多图层:
https://blog.csdn.net/sunsoda/article/details/82431770

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值