文章目录
- 1 pyplot.figure( )
- 语法
- 参数
- 测试
- figsize
- facecolor
- edgecolor
- frameon
- 2 pyplot.subplot( )
- 说明
- 设置所有子图的大标题
- 分别设置每个子图的标题
- 3 pyplot.legend( )
- 作用
- 设置图例位置
- 设置图例边框
- 设置图例边框颜色
- 设置图例背景颜色
- 设置图例标题
- 4 绘制三维图像
- 利用关键字`projection='3d'`来实现三维效果
- 从`mpl_toolkits.mplot3d`导入对象`Axes3D`实现三维效果
- 绘制三维曲线
- 绘制三维散点图
- 绘制三维曲面
- zdim
- rstride/cstride
- alpha
- zdir
- set_xlabel/set_ylabel/set_zlabel
- scatter
- 参考
1 pyplot.figure( )
语法
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
参数
- num:图像编号或名称,数字为编号 ,字符串为名称
- figsize:指定figure的宽和高,单位为英寸;
- dpi参数指定绘图对象的分辨率,即每英寸多少个像素,默认值为80 (1英寸等于2.5cm,A4纸是 21*30cm的纸张 )
- facecolor:背景颜色
- edgecolor:边框颜色
- frameon:是否显示边框/表示是否绘制窗口的图框
测试
figsize
默认时
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
设置为figsize=(4,6)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=(4,6))
plt.plot(x,y)
plt.show()
设置为figsize=(8,6)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=(8,6))
plt.plot(x,y)
plt.show()
设置为figsize=(12,6)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=(12,6))
plt.plot(x,y)
plt.show()
设置为figsize=(8,3)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=(8,3))
plt.plot(x,y)
plt.show()
设置为figsize=(8,9)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(figsize=(8,9))
plt.plot(x,y)
plt.show()
facecolor
默认状态:
将背景色设置为红色
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(facecolor="red")
plt.plot(x,y)
plt.show()
将背景色设置为#00cec9
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(facecolor="#00cec9")
plt.plot(x,y)
plt.show()
edgecolor
如果只是单纯的设置edgecolor
会发现并没有起作用
这是因为默认状态下linewidth=0
所以只需要设置一下linewidth
,使其不为0 ,就可以使edgecolor
生效
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(linewidth=5.0,edgecolor="red")
plt.plot(x,y)
plt.show()
frameon
这里需要先设置linewidth=5.0,edgecolor="red"
便于观察
不然默认情况下,边框不好观察出来
frameon默认为True
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(linewidth=5.0,edgecolor="red",frameon=True)
plt.plot(x,y)
plt.show()
设置为False时,边框不显示
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
plt.figure(linewidth=5.0,edgecolor="red",frameon=False)
plt.plot(x,y)
plt.show()
2 pyplot.subplot( )
说明
subplot(121) 表示整个区域分为一行,两列,一共可以容纳2张子图,此子图位于第一个位置
前面两个数字表示分为几行几列,后一个数字表示第几个位置。
排列顺序:从左到右,从上到下
subplot(122) 表示此子图位于第二个位置
举例:subplot(122) 位于下图中蓝色位置
subplot(223) 位于下图中蓝色位置
Demo代码
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.figure(figsize=(12,6))
# 子图1
plt.subplot(1,2,1)
plt.plot(x,y)
# 子图2
plt.subplot(1,2,2)
plt.plot(x,z)
plt.show()
设置所有子图的大标题
pyplot.suptitle() 用于设置整个区域的标题(多个子图的情况下)
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.figure(figsize=(12,6))
plt.suptitle("全局标题") # 全局标题
# 子图1
plt.subplot(1,2,1)
plt.plot(x,y)
# 子图2
plt.subplot(1,2,2)
plt.plot(x,z)
plt.show()
分别设置每个子图的标题
ax = plt.subplot(122)
ax.set_title("子图2的标题")
Demo代码
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.figure(figsize=(12,6))
plt.suptitle("全局标题") # 全局标题
# 子图1
plt.subplot(1,2,1)
ax = plt.subplot(121)
ax.set_title("子图1的标题")
plt.plot(x,y)
# 子图2
plt.subplot(1,2,2)
ax = plt.subplot(122)
ax.set_title("子图2的标题")
plt.plot(x,z)
plt.show()
注: 还可以这样写 ax = plt.subplot(221+i)
3 pyplot.legend( )
作用
为图像加图例
首先需要在plt.plot( )中的参数label=’’“设置图例的名称
Demo代码
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")# 图例名称有:sin(x)
plt.plot(x,z,label="cos(x)")# 图例名称有:cos(x)
plt.legend()# 添加图例
plt.show()
设置图例位置
图例位置默是系统自动选择最佳位置,也就是参数
loc='best'
使用参数loc进行设置图例的具体位置,一般可以设置为:
- upper left
- upper center
- upper right
- center left
- center
- center right
- lower left
- lower center
- lower right
也可以设置为loc=(x,y) 其中x、y分别表示占x轴、y轴的比例,都是以(0,0)为坐标原点(个人理解哈)
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")
plt.plot(x,z,label="cos(x)")
plt.legend(loc=(0.5,0.5),facecolor='yellow') # 图例位于距x轴50%(相对于x
轴总长度)、距y轴50%, 背景色为yellow
plt.show()
设置图例边框
frameon=False
去掉图例边框 (True为显示,默认为True)
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")
plt.plot(x,z,label="cos(x)")
plt.legend(loc='best',frameon=False) #去掉图例边框
plt.show()
设置图例边框颜色
edgecolor='red'
边框设置为红色
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")
plt.plot(x,z,label="cos(x)")
plt.legend(loc='best',edgecolor='red') # 边框设置为红色
plt.show()
设置图例背景颜色
facecolor='yellow'
设置图例背景颜色
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")
plt.plot(x,z,label="cos(x)")
plt.legend(loc='best',facecolor='yellow') #设置图例背景颜色,若无边框,参数无效
plt.show()
设置图例标题
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,label="sin(x)")
plt.plot(x,z,label="cos(x)")
plt.legend(title="图例的标题") # 设置图例标题
plt.show()
4 绘制三维图像
创建Axes3D主要有两种方式
- 一种是利用关键字projection='3d’l来实现
- 另一种则是通过从mpl_toolkits.mplot3d导入对象Axes3D来实现
目的都是生成具有三维格式的对象Axes3D.
利用关键字projection='3d'来实现三维效果
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d') #!
ax.plot_surface(x, y, z, rstride=1,cstride=1)#!
plt.show()
从mpl_toolkits.mplot3d导入对象Axes3D实现三维效果
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D #需要导入Axes3D
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = Axes3D(fig)#!
ax.plot_surface(x, y, z, rstride=1,cstride=1)#!
plt.show()
绘制三维曲线
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D #需要导入Axes3D
z = np.linspace(0,10,100)
x = 5 * np.sin(z)
y = 5 * np.cos(z)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot3D(x,y,z)#绘制空间曲线
plt.show()
绘制三维散点图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D #需要导入Axes3D
z = 10 * np.random.random(100)
x = 5 * np.sin(z)
y = 5 * np.cos(z)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter3D(x,y,z)#绘制三维散点图
plt.show()
绘制三维曲面
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d') #!
ax.plot_surface(x, y, z, rstride=1,cstride=1)#!
plt.show()
zdim
绘制等高线图 并将其投影到z轴数值为10处
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
rstride/cstride
如果加入渲染时的步长,会得到更加清晰细腻的图像,参数rstride、cstride
分别表示row(行)和column(列)上的绘图采样步长,越小绘图越精细
rstride=10,cstride=10
时
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
rstride=2,cstride=2
时
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=2,cstride=2)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
alpha
alpha=0.1
控制透明度为0.1
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=.1)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
alpha=0.5
控制透明度为0.5
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=.5)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
alpha=1
控制透明度为1
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=1)
ax.contour(x,y,z,zdim='z',offset=10)
plt.show()
zdir
控制投影方向
zdir='z’投影z方向的图像,投影图像在x-y平面
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=1)
ax.contour(x,y,z,zdim='z',offset=10,zdir='z')
plt.show()
zdir='x’投影z方向的图像,投影图像在y-z平面
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=1)
ax.contour(x,y,z,zdim='z',offset=10,zdir='x')
plt.show()
zdir='y’投影y方向的图像,投影图像在x-z平面
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=1)
ax.contour(x,y,z,zdim='z',offset=10,zdir='y')
plt.show()
set_xlabel/set_ylabel/set_zlabel
set_xlabel设置x轴label
set_ylabel设置y轴label
set_zlabel设置z轴label
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# 配置中文(这里依据自己的实际情况而定)
font = {
"family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x,y = np.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,rstride=10,cstride=10,alpha=1)
ax.contour(x,y,z,zdim='z',offset=10,zdir='z')
ax.set_xlabel('x轴')#设置x轴label
ax.set_ylabel('y轴')#设置y轴label
ax.set_zlabel('z轴')#设置z轴label
plt.show()
scatter
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
c:颜色或色彩序列
s:点的大小 标量或形如shape(n,)
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#定义坐标轴
fig4 = plt.figure()
ax4 = plt.axes(projection='3d')
#生成三维数据
xx = np.random.random(20)*10-5 #取20个随机数,范围在-5~5之间
yy = np.random.random(20)*10-5
X, Y = np.meshgrid(xx, yy) # 20*20 组成400个点
Z = np.sin(np.sqrt(X**2+Y**2))
#作图
ax4.scatter(X,Y,Z,alpha=0.3,c=np.random.random(400),s=np.random.randint(10,20, size=(20, 20))) #生成散点.利用c控制颜色序列,s控制大小
# np.random.randint(10,20, size=(20, 20)) 随机数范围为10-20 shape为20 * 20
# c=np.random.random(400) 随机生成400个随机数字 范围为0-1 shape(400,)
plt.show()
参考